下面是我的listview customAdapter类getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
HashMap<String, Object> cTa= new HashMap<String, Object>();
cTa= d.getPosition(position)
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
wrapper.getButton().setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//operation-1
}
});
return row;
}
我必须在操作-1 区域显示消息,从 cTa 获取数据。在操作1区域上只允许最终变量。但是根据定义,cTa不能是最终的。所以请帮助我如何在这里获取访问权限cTa。或任何其他替代品。谢谢!
答案 0 :(得分:0)
试试这种方式
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
wrapper.getButton().setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
HashMap<String, Object> cTa= new HashMap<String, Object>(d.getPosition(position));
// Your operation
}
});
return row;
}
答案 1 :(得分:0)
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
HashMap<String, Object> cTa= new HashMap<String, Object>();
cTa= d.getPosition(position)
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
wrapper.getButton.setTag(cTa);
wrapper.getButton().setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
HashMap<String, Object> cTa = (HashMap)v.getTag();
}
});
return row;
}
答案 2 :(得分:0)
这对我来说非常有用
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listview;
List<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareData();
listview =new ListView(this);
MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.activity_item,new String[] {"uu"},new int[]{R.id.showTv});
listview.setAdapter(adapter);
setContentView(listview);
}
private void prepareData(){
data=new ArrayList<Map<String,Object>>();
Map<String,Object> item;
item=new HashMap<String,Object>();
item.put("din", "hello");
data.add(item);
item=new HashMap<String,Object>();
item.put("bin", "world");
data.add(item);
item=new HashMap<String,Object>();
item.put("tin", "rock");
data.add(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
适配器类
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MySimpleAdapter extends SimpleAdapter {
private final Context context;
private List<Map<String, Object>> data;
private int resource;
private String[] from;
private int[] to;
public MySimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,int[] to) {
super(context, data, resource, from, to);
this.context=context;
this.data=(List<Map<String, Object>>) data;
this.resource=resource;
this.from=from;
this.to=to;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View rowView = inflater.inflate(resource, null, true);
Map<String,Object> medMap = data.get(position);
final TextView[] showTv = new TextView[from.length];
Button btn1 = (Button)rowView.findViewById(R.id.gointoBt);
Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(context, "my pos " + "button1 "+ position, Toast.LENGTH_LONG).show();
}
};
btn1.setOnClickListener(mOkOnClickListener);
Button btn2=(Button)rowView.findViewById(R.id.chooseBt);
Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(context, "my pos " + "button2 "+ position, Toast.LENGTH_LONG).show();
}
};
btn2.setOnClickListener(mOkOnClickListener2);
return rowView;
}
}
玩得开心..