사용자 지정 목록보기를 만들었으며 각 행에 대해 onItemClick Listener를 추가하려고했지만 아무런 문제없이 수행하는 방법을 모릅니다. 다음은 코드입니다. ListView의 별도 개체를 수동으로 만든 다음 OnitemClick Listener를 추가하여이 작업을 시도했지만 작동하지 않습니다. Android customListView의 각 행에 대해 OnItemListener를 추가하는 방법은 무엇입니까?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_main"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.saadreviews.health_plus.Calories_Activity$PlaceholderFragment" >
<TextView
android:id="@+id/txtv_calories"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="3dp"
android:text="Total Calories = "
android:textSize="21sp"
android:textColor="#006600"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtv_calories"
android:layout_marginTop="5dp"
android:background="@drawable/line" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="317dp"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:layout_marginTop="14dp"
android:background="@drawable/bg_list" >
</ListView>
<ImageView
android:id="@+id/bttn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="reset"
android:background="@drawable/custom_reset" />
다음은 javaFile입니다.
public class Calories_Activity extends ListActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calories);
this.getActionBar().hide();
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textView_food, getResources().getStringArray(R.array.food)));
setListAdapter(new MyAdapter(this,
android.R.layout.simple_list_item_1, R.id.textView_calories,
getResources().getStringArray(R.array.calories)));
}
public void reset(View view)
{
Toast.makeText(getBaseContext(),
"Reset", Toast.LENGTH_SHORT).show();
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.food);
String[] calo = getResources().getStringArray(R.array.calories);
ImageView iv= (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView_food);
TextView tv1 = (TextView) row.findViewById(R.id.textView_calories);
tv.setText(items[position]);
tv1.setText(calo[position]);
if(items[position].equals("Apple"))
{
iv.setImageResource(R.drawable.apple);
}
else if(items[position].equals("Burger"))
{
iv.setImageResource(R.drawable.burger);
}
else if(items[position].equals("Mango"))
{
iv.setImageResource(R.drawable.mango);
}
else if(items[position].equals("Orange"))
{
iv.setImageResource(R.drawable.orange);
}
else if(items[position].equals("Pizza"))
{
iv.setImageResource(R.drawable.pizza);
}
return row;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calories_, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i2 = new Intent(Calories_Activity.this, Father_Activity.class);
startActivity(i2);
}
}
원시 레이아웃 파일을 보여줄 수 있습니까? list_item 레이아웃? –
다음은 코드입니다. onitemclick 리스너를 추가하도록 도와주세요. 그냥 토스트 아이템의 이름. http://snk.to/f-ct3aoijt –