当我点击列表视图的项目时没有任何反应这里是代码
@SuppressLint("ParserError")
public class DinerList extends Activity implements Comparator<CanteenTagEntry>,
OnItemSelectedListener {
List<CanteenTagEntry> dinerlist;
int size;
int[] arr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diner_list);
ListView listView1 = (ListView) findViewById(R.id.list);
Context con = getApplicationContext();
DatabaseHelper dbHelper = new DatabaseHelper(con);
ICanteenLogRepository repository = dbHelper.getCanteenLogRepository();
try {
dinerlist = repository.total();
Collections.sort(dinerlist, new DinerList());
Collections.sort(dinerlist, new sortbyuser());
Collections.sort(dinerlist, new sortbycompany());
size = dinerlist.size();
arr = new int[size];
for (int i = 0; i < size; i++) {
CanteenTagEntry tagentry = dinerlist.get(i);
int b = tagentry.getId();
System.out.print(b);
arr[i] = tagentry.getId();
System.out.print(arr[i]);
}
} catch (SQLException e) {
Log.d("DinerList", "Unable to retrieve diner list", e);
}
CanteenAdapter adapter = new CanteenAdapter(this,
R.layout.listview_item_row,
(ArrayList<CanteenTagEntry>) dinerlist);
listView1.setAdapter(adapter);
}
public class CanteenAdapter extends ArrayAdapter<CanteenTagEntry> {
Context context;
int layoutResourceId;
ArrayList<CanteenTagEntry> data = (ArrayList<CanteenTagEntry>) dinerlist;
public CanteenAdapter(Context context, int layoutResourceId,
ArrayList<CanteenTagEntry> data) {
super(context, layoutResourceId, dinerlist);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = (ArrayList<CanteenTagEntry>) dinerlist;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RouteHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RouteHolder();
holder.txtName1 = (TextView) row.findViewById(R.id.Title1);
holder.txtName2 = (TextView) row.findViewById(R.id.Title2);
holder.txtName3 = (TextView) row.findViewById(R.id.Title3);
row.setTag(holder);
} else {
holder = (RouteHolder) row.getTag();
}
CanteenTagEntry tagentry = data.get(position);
String idcolor = tagentry.getUser().getIdcolor().toString();
try {
holder.txtName1.setTextColor(Color.parseColor(idcolor));
holder.txtName2.setTextColor(Color.parseColor(idcolor));
holder.txtName3.setTextColor(Color.parseColor(idcolor));
} catch (Exception e) {
holder.txtName1.setTextColor(Color.GRAY);
holder.txtName2.setTextColor(Color.GRAY);
holder.txtName3.setTextColor(Color.GRAY);
}
holder.txtName1.setText(tagentry.getUser().getUserType().getName());
holder.txtName2.setText(tagentry.getUser().getCompany().getName());
holder.txtName3.setText(tagentry.getUser().getFirstName());
return row;
}
class RouteHolder {
TextView txtName1;
TextView txtName2;
TextView txtName3;
TextView txtName4;
}
}
@Override
public int compare(CanteenTagEntry o1, CanteenTagEntry o2) {
return o1.getUser().getFirstName()
.compareTo(o2.getUser().getFirstName());
}
public class sortbyuser implements Comparator<CanteenTagEntry> {
@Override
public int compare(CanteenTagEntry lhs, CanteenTagEntry rhs) {
return lhs.getUser().getUserType().getName()
.compareTo(rhs.getUser().getUserType().getName());
}
}
public class sortbycompany implements Comparator<CanteenTagEntry> {
@Override
public int compare(CanteenTagEntry lhs, CanteenTagEntry rhs) {
return lhs.getUser().getCompany().getName()
.compareTo(rhs.getUser().getCompany().getName());
}
}
@Override
public void onItemSelected(AdapterView<?> l, View v, int position,
long ide) {
super.onListItemClick(l, v, position, ide);
CanteenTagEntry tagentry = null;
int pos = position;
int id = arr[pos];
int userid = 0;
Context con = getApplicationContext();
DatabaseHelper dbHelper = new DatabaseHelper(con);
ICanteenLogRepository repository = dbHelper.getCanteenLogRepository();
try {
tagentry = repository.Retrieve(id);
userid = tagentry.getUser().getId();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
StartFood food = new StartFood(id, userid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
我很瘦,你需要使用
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// do your work here
}
});
答案 1 :(得分:1)
尝试按照下面的listView1
设置OnItemClickListener
并尝试运行。
listView1.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
CanteenTagEntry tagentry = null;
int pos = position;
int id = arr[pos];
int userid = 0;
Context con = getApplicationContext();
DatabaseHelper dbHelper = new DatabaseHelper(con);
ICanteenLogRepository repository = dbHelper.getCanteenLogRepository();
try
{
tagentry = repository.Retrieve(id);
userid = tagentry.getUser().getId();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
StartFood food = new StartFood(id, userid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
在返回行之前将侦听器设置为这样的行。
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// your code
}
});