活动代码:
package com.example.tayyab.myapplication;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyProfile extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_profile);
populateListView();
}
private void populateListView() {
String[] myItems = {"Missions","Messages","Camera","Gallery","Temprature","Humidity"};
int[] img = {R.drawable.target,R.drawable.chat,R.drawable.camera,R.drawable.gallery,R.drawable.temp,R.drawable.humidity};
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,myItems);
//ArrayAdapter<Integer> ad = new ArrayAdapter<Integer>(this,R.layout.list_item,img);
final ProfileAdapter adapter = new ProfileAdapter(getApplicationContext(),R.layout.list_item);
int i=0;
for(String Name : myItems){
Profile obj = new Profile(Name,img[i]);
adapter.add(obj);
i++;
}
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
list.setClickable(true);
ImageButton b3 = (ImageButton)findViewById(R.id.imageButton);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MyProfile.this,MainMission.class);
startActivity(intent);
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("Log ","Inside Click Listener");
String myItems = (String) adapterView.getItemAtPosition(i);
Toast.makeText(view.getContext(),myItems,Toast.LENGTH_SHORT).show();
if(i==1) {
Log.i("Message Event", "Inside Message Activity");
Intent a = new Intent(MyProfile.this, MainMission.class);
startActivity(a);
}
}
});
}
}
适配器类:
package com.example.tayyab.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ProfileAdapter extends ArrayAdapter {
private List list = new ArrayList();
public ProfileAdapter(Context context, int resource) {
super(context, resource);
}
public void add(Profile object) {
list.add(object);
super.add(object);
}
static class ImageHolder {
ImageView img;
TextView txt;
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
ImageHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
holder = new ImageHolder();
holder.img = (ImageView) row.findViewById(R.id.profile_images);
holder.txt = (TextView) row.findViewById(R.id.profile_names);
row.setTag(holder);
}
else {
holder = (ImageHolder) row.getTag();
}
Profile pr = (Profile) getItem(position);
holder.img.setImageResource(pr.getImg_names());
holder.txt.setText(pr.getItem_names());
return row;
}
}
答案 0 :(得分:0)
也许你想让监听器监听ListView项目的点击。你在那里做的不是一个正确的方法。
您需要将onItemClickListener添加到ListView
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Toast.makeText(getContext(), "Item #" + position, Toast.LENGTH_SHORT).show();
}
});
你有一个基本的监听器,可以告诉你你点击了什么项目。您可以在onItemClick
方法中执行逻辑。
另外,如果您为ListView的行使用自定义布局,则需要添加
android:descendantFocusability="blocksDescendants"
在列表中的第一个父布局上。
另外,请考虑使用RecyclerView而不是ListView。