我正在使用基础适配器,我有列表,其中包含带有文本视图的列表(表示左侧已发送项目,下方为该项,右侧收到项目,我想点击列表中的每个位置执行回复消息传递。
请建议我如何执行单击listitem
的功能以下是我正在使用的代码
类MessageAdapter扩展了BaseAdapter {
ViewHolder viewHolder;
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
public int getCount() {
// TODO Auto-generated method stub
return Constant_Variables.Sms_Status_list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.messagelist_row, null);
viewHolder=new ViewHolder();
viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.sms_from_row.setVisibility(View.VISIBLE);
viewHolder.sms_to_row.setVisibility(View.VISIBLE);
if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_sent))
{
viewHolder.sms_from_row.setVisibility(View.GONE);
viewHolder.sms_to_row.setText("Sent: "+Constant_Variables.Sms_from_list.get(position));
}
else if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_received))
{
viewHolder.sms_to_row.setVisibility(View.GONE);
viewHolder.sms_from_row.setText("Received: "+Constant_Variables.Sms_to_list.get(position));
}
else
{
viewHolder.sms_from_row.setText("Message in queued.");
viewHolder.sms_to_row.setVisibility(View.VISIBLE);
}
convertView.setTag(viewHolder);
return convertView;
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
if(l.callOnClick()== true)
{
Log.i("the Item clicked is at position : ", ""+ position);
}
else
{
Log.i("the Item not clicked is at position : ","the Item not clicked is at position" );
}
}
}
以下是我正在使用的XML代码
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/sms_from_txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_alignParentRight="true"
android:background="@drawable/blue"
android:layout_alignParentTop="true"
android:text="From" />
<TextView
android:id="@+id/sms_sendto_txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:background="@drawable/red"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="To" />
</RelativeLayout >
此致 Amrinder Singh
答案 0 :(得分:0)
在android中有onItemClickListener,你可以使用那个
ListView listview = (ListView)findViewById(R.id.yourlist);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// @todo
}
});
对于列表项中的单个视图,您始终可以在getView方法中添加单击侦听器。
答案 1 :(得分:0)
初始化该视图时实现两者的点击监听器
viewHolder.sms_from_row.setOnClickListener(new OnClickListener(){
public void onClick(View view){
// Todo thing
}
});
viewHolder.sms_to_row.setOnClickListener(new OnClickListener(){
public void onClick(View view){
// Todo thing
}
});
答案 2 :(得分:0)
您的代码是正确的,只需在此下方添加点击监听器即可。
viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);
在此下面添加点击监听器。
viewHolder.sms_from_row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
viewHolder.sms_to_row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
希望对你有所帮助。
感谢。
答案 3 :(得分:0)
在项目上点击textview
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.messagelist_row, null);
viewHolder=new ViewHolder();
viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);
viewHolder.sms_from_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.sms_from_row.setVisibility(View.VISIBLE);
viewHolder.sms_to_row.setVisibility(View.VISIBLE);
if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_sent))
{
viewHolder.sms_from_row.setVisibility(View.GONE);
viewHolder.sms_to_row.setText("Sent: "+Constant_Variables.Sms_from_list.get(position));
}
else if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_received))
{
viewHolder.sms_to_row.setVisibility(View.GONE);
viewHolder.sms_from_row.setText("Received: "+Constant_Variables.Sms_to_list.get(position));
}
else
{
viewHolder.sms_from_row.setText("Message in queued.");
viewHolder.sms_to_row.setVisibility(View.VISIBLE);
}
convertView.setTag(viewHolder);
return convertView;
}
答案 4 :(得分:0)
点击列表时
点击删除图标
以下是您可以参考的代码:
public class CaseAdapter extends BaseAdapter{
Context context;
ArrayList<PojoClass> list;
LayoutInflater inflater;
public CaseAdapter(Context context,ArrayList<PojoClass> list)
{
this.context=context;
this.list=list;
inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null)
{
row = inflater.inflate(R.layout.activity_home_screen, parent,false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
holder.caseNumber.setText(list.get(position).getCaseNo());
holder.state.setText(list.get(position).getState());
holder.date.setText(list.get(position).getDate());
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "record clicked!", Toast.LENGTH_LONG).show();
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v=(View) v.getParent();//utilizing the View object...or we can use new view object
//View view=(View)v.getParent();//this one also we can use...but instead of v object you need to use view object;
TextView number=(TextView)v.findViewById(R.id.textView_case_number);
InfoDb db=new InfoDb(context);//using for remove record from database
db.open();
if(db.removeRecord(number.getText().toString()))
{
list.remove(position);
Toast.makeText(context, "Record Deleted!", Toast.LENGTH_LONG).show();
CaseAdapter.this.notifyDataSetChanged();
}else
Toast.makeText(context, "Record not Deleted!", Toast.LENGTH_LONG).show();
db.close();
}
});
return row;
}
public class ViewHolder{
TextView state,caseNumber,date;
ImageView delete;
public ViewHolder(View v){
state=(TextView)v.findViewById(R.id.textView_State);
caseNumber=(TextView)v.findViewById(R.id.textView_case_number);
date=(TextView)v.findViewById(R.id.textView_date);
delete=(ImageView)v.findViewById(R.id.ImageView_Delete);
}
}
}