自定义listview的java Android输出值出现错误
问题:试图输出所选listview的描述。下面是我的代码,我还有一个listview的示例以及创建它的代码。我有一个显示三个值的自定义列表视图,当我单击第二行(3/12/04 Gas$60.00)时,我希望它输出描述(“Gas”)
onItemClick
是我的问题所在ItemMenuActivity
谢谢你的帮助和时间
listview中的示例数据:
3/12/04 New Shoes $50.00
3/12/04 Gas $60.00
3/12/04 Food $10.00
ItemMenuActivity。爪哇
public class ItemMenuActivity extends Activity implements AdapterView.OnItemClickListener
{
String accountName;
ArrayList<Item> item_details;
DatabaseHandler database;
ListView itemView;
private EditText dateEditText, costEditText, desEditText;
private Spinner categorySpinner;
private Button btnAddItem, btnCancel;
private boolean errlvl;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.item_menu_layout);
// Initiate Database
database = new DatabaseHandler(getApplicationContext());
// Initiate/configure ListView
itemView = (ListView)findViewById(R.id.itemListView);
itemView.setOnItemClickListener(this);
Bundle bundle = getIntent().getExtras();
String account_name = bundle.getString("AccountName");
setTitle(account_name);
accountName = account_name;
displaySpecificItemListView(accountName);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//TextView editTextDescription = ((TextView)view.findViewById(R.id.editTextDescription));
//String temp = editTextDescription.getText().toString();
//Toast.makeText(this, temp, Toast.LENGTH_LONG).show();
}
}
添加项目布局。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/addItemLayout"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:orientation="vertical"
安卓:padding="10dp" >
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal">
<TextView
安卓:id="@+id/textViewDate"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:padding="10dp"
安卓:text="@string/purchDateTitle"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"/>
<EditText
安卓:id="@+id/editTextDate"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:padding="10dp"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical">
<EditText
安卓:id="@+id/editTextDescription"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:padding="10dp"
安卓:hint="Enter Item Description"
安卓:textAppearance="?安卓:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="$ "
安卓:textSize="20dp"/>
<EditText
安卓:id="@+id/editTextCost"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:padding="10dp"
安卓:hint="Enter Total Cost"
安卓:textAppearance="?安卓:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical">
<Spinner
安卓:id="@+id/categorySelectSpinner"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:padding="10dp"
安卓:orientation="horizontal">
<Button
安卓:id="@+id/btnAddItem"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="Add" />
<Button
安卓:id="@+id/btnCancelItem"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="Cancel" />
</LinearLayout>
</LinearLayout>
ItemListViewBaseAdapter。爪哇
public class ItemListViewBaseAdapter extends BaseAdapter
{
private ArrayList<Item> _data;
Context _c;
ItemListViewBaseAdapter (ArrayList<Item> data, Context c)
{
_data = data;
_c = c;
}
public int getCount()
{
// TODO Auto generated method stub
return _data.size();
}
public Object getItem(int position)
{
// TODO Auto generated method stub
return _data.get(position);
}
public long getItemId(int position)
{
// TODO Auto generated method stub
return position;
}
public View getView (int position, View convertView, ViewGroup parent)
{
// TODO Auto generate method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_item_listview_layout, null);
}
TextView itemDate = (TextView)v.findViewById(R.id.dateTextView);
TextView itemDescription = (TextView)v.findViewById(R.id.descriptionTextView);
TextView itemAmount = (TextView)v.findViewById(R.id.amountTextView);
Item accMsg = _data.get(position);
itemDate.setText(accMsg.entry_date);
itemDescription.setText(accMsg.item_description);
//NumberFormat format
if(accMsg.item_price > 0)
{
String myString = String.format("%.2f", accMsg.item_price);
String FormattedString = "$"+myString;
itemAmount.setText(FormattedString);
}
else
{
double temp = Math.abs(accMsg.item_price);
String myString = String.format("%.2f", temp);
String FormattedString = "-$"+myString;
itemAmount.setText(FormattedString);
itemAmount.setTextColor(Color.parseColor("#088A08"));
}
return v;
}
}
项目菜单布局。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical" >
<ListView
安卓:id="@+id/itemListView"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:dividerHeight="0.1dp"
安卓:divider="#81A594"
安卓:layout_below="@+id/dateTitle"/>
</RelativeLayout>
# 1 楼答案
在
onItemClick()
方法中,您正在执行获取您的文本视图。这与您发布的XML(add_item_layout.XML)匹配,但您的适配器代码说明了不同的情况
在适配器中,对于新行,您正在膨胀
custom_item_listview_layout
,您的“描述”文本视图ID为R.id.descriptionTextView
因此,如果您在
onItemClick()
中进行此编辑,应该可以解决您的问题: