我正在使用带有自定义适配器的ListView
。我每行内有两个TextView
。我需要仅为我单击的textView更改这些TextView
的文本。我怎样才能实现它?
我在CustomAdapter
内部有一个方法,我在其中初始化TextView
的
public class MainListAdapter extends BaseAdapter {
private static final String TAG = "MainListAdapter";
private Context mContext;
private LayoutInflater layoutInflater;
private MyViewPager itemViewPager;
private View viewMain;
private View viewSlide;
private TextView cancel;
private TextView delete, block;
TextView itemName, showResult;
int triedOnce;
private ArrayList<View> views;
private PagerAdapter pagerAdapter;
private ArrayList<String> mList;
public static final String API_KEY = "MYAPIKEY";
public MainListAdapter(Context context, ArrayList<String> list) {
mContext = context;
layoutInflater = LayoutInflater.from(mContext);
mList = list;
}
@Override
public int getCount() {
return mList != null ? mList.size() : 0;
}
@Override
public Object getItem(int position) {
return mList != null ? mList.get(position) : null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_item, null);
}
if(position%2 == 0){
convertView.setBackgroundResource(R.drawable.lis_bg);
}else if(position%2 == 1){
convertView.setBackgroundResource(R.drawable.ls_eppadi_bg);
}
viewMain = layoutInflater.inflate(R.layout.listview_item_main, null);
viewSlide = layoutInflater.inflate(R.layout.listview_item_slide, null);
cancel = (TextView)viewSlide.findViewById(R.id.tv_menu_cancel);
delete = (TextView)viewSlide.findViewById(R.id.tv_menu_delete);
block = (TextView) viewSlide.findViewById(R.id.tv_menu_block);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//perform cancel
}
});
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//perform delete
}
});
block.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//perform block
}
});
views = new ArrayList<View>();
views.add(viewMain);
views.add(viewSlide);
itemViewPager = (MyViewPager)convertView.findViewById(R.id.vp_list_item);
itemViewPager.setSelfIndex(position);
pagerAdapter = new PagerAdapter() {
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((MyViewPager)container).removeView(views.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
((MyViewPager)container).addView(views.get(position));
return views.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getCount() {
return views.size();
}
};
fillItemData(convertView, position, viewMain);
itemViewPager.setAdapter(pagerAdapter);
itemViewPager.setCurrentItem(0);
return convertView;
}
//this is where the textview's are initialised
private void fillItemData(View convertView, final int position, View viewMain) {
int[] colorCollection = {
R.color.green, R.color.royalblue, R.color.violet
};
for (int i = 0; i < colorCollection.length; i++) {
colorCollection[i] = mContext.getResources().getColor(colorCollection[i]);
}
int currColor = colorCollection[position % colorCollection.length];
itemName = (TextView)viewMain.findViewById(R.id.tv_item_name);
showResult = (TextView)viewMain.findViewById(R.id.tv_show);
itemName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new PushTask().execute(position);
triedOnce = 0;
}
});
itemName.setBackgroundColor(currColor);
showResult.setBackgroundColor(currColor);
itemName.setText(mList.get(position));
}
class PushTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected void onPreExecute() {
itemName.setVisibility(View.GONE);
showResult.setVisibility(View.VISIBLE);
//set text here based on the position
showResult.setText("SENDING");
}
@Override
protected Integer doInBackground(Integer... position) {
int post = position[0];
int respCode = 0;
//perform my network operations
return respCode;
}
@Override
protected void onPostExecute(Integer response) {
switch(response) {
case 1:
//set text here
showResult.setText("SENT");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
showResult.setVisibility(View.GONE);
itemName.setVisibility(View.VISIBLE);
}
}, 2000);
break;
case 2:
//set text here
showResult.setText("PLEASE WAIT!");
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
showResult.setVisibility(View.GONE);
itemName.setVisibility(View.VISIBLE);
}
}, 2000);
break;
case 3:
//set text here
showResult.setText("FAILED!");
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
showResult.setVisibility(View.GONE);
itemName.setVisibility(View.VISIBLE);
}
}, 2000);
break;
case 4:
//set text here
showResult.setText("FAILED!");
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
showResult.setVisibility(View.GONE);
itemName.setVisibility(View.VISIBLE);
}
}, 2000);
break;
default:
break;
}
}
}
}
现在,我需要根据用户点击的位置更改itemName
和showResult
的文字。 itemName.setOnClickListener
有AsyncTask,我将更改这些textView的文本。
目前它更改为listView的最后一行。
如何更改所选位置。
提前致谢。
答案 0 :(得分:0)
覆盖CustomAdapter的getView(...)函数,其中一个参数是int位置。
请勿忘记使用convertView,holder和settag / gettag进行平滑滚动和正确引用
更新
在getView中输入:
.val()
我们还需要在postExecute()中引用convertView。您可以在AsycTask参数中传递此引用,或者为适配器设置getter setter,并在asyctask postexecute()中单击引用
答案 1 :(得分:0)
首先创建一个模型(POJO)类 使用您的两个字符串和一个布尔值将其称为isChecked 添加getter和setter到它
将值(列表数据)设置为arraylist并首次将setIsChecked值保持为false。像这样
ArrayList<model> list = new ArrayList<model>();
list.add(new model("string1","string2",false);
list.add(new model("string11","string22",false);
等等
现在onItemClickListener像这样使用循环
for(int i = 0 ; i < list.size; i++){
list.get(i).setIsChecked(false);
}
list.get(itemClickedPosition).setIsChecked(true);
youradapter.notifyDataSetChanged();
之后现在进入适配器类,您可以根据需要设置所选的textview
getView方法中的执行此操作
if(list.get(position).getIsChecked()){
change your textview
}else{
leave as it is
}
完成让我知道它是否有帮助
答案 2 :(得分:-2)
Pass View in Async Task and then convert into textView and SetText in DoInBackground Method.
new PushTask().execute(v);
@Override
protected Integer doInBackground(final View position) {
runOnUiThread(new Runnable() {
public void run() {
// some code #3 (Write your code here to run in UI thread)
TextView txtName=(TextView)position;
txtname.setText(“android”):
}
});
}