詢問問題並花費15天解決此問題後,我再次尋找幫助和解決方案。 在MainActivity中,我創建了Json Downloading Task,它從http下載數據,並使用CustomListAdapter.class填充listview。 一切正常。 現在,在列表視圖中,我有2個textview,我希望是可點擊的,其中一個是「接受」,textview只是在xml中沒有填充適配器或Json。 「接受」應該像這樣工作「將文本更改爲接受並更改顏色」及其工作方式與其他所有方式一樣。但當我點擊第一個「接受」(位置0)在列表視圖 它改變其他列表視圖項目(位置4,9)。這就像我點擊位置4,9的文字。 第一張圖片是在點擊「接受」之前,第二張圖片是在點擊之後。ListView子對象可點擊確認
///
public class MainActivity extends Activity {
protected static final String TAG = null;
public ArrayList<FeedItem> feedList;
public ListView feedListView;
private ProgressBar progressbar;
private CustomListAdapter adap;
private LayoutInflater mInflater;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feedListView= (ListView) findViewById(R.id.custom_list);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
String url = "...";
new DownloadFilesTask().execute(url);
getActionBar().setIcon(R.drawable.angel);
progressbar = (ProgressBar)findViewById(R.id.progressBar);
public void updateList() {
adap = new CustomListAdapter(this, feedList);
feedListView.setAdapter(adap);
}
public class DownloadFilesTask extends AsyncTask<String, Integer, Void> {
///....
CustomListAdapter.class
public class CustomListAdapter extends BaseAdapter
{
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
private ArrayList<String> data;
protected ListView feedListView;
ArrayList<HashMap<String,String>> list;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData)
{
this.listData = listData;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("Sample Text " + String.valueOf(i));
}
}
@Override
public int getCount()
{
return listData.size();
}
@Override
public Object getItem(int position)
{
return listData.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View row=convertView;
if ((row == null) || (row.getTag()==null)) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.accept= (TextView) convertView.findViewById(R.id.acceptTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
final FeedItem newsItem = (FeedItem) listData.get(position);
holder.accept.setFocusable(true);
holder.accept.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
holder.accept.setText(Html.fromHtml(newsItem.getContent()));
}
});
return convertView;
}
static class ViewHolder
{
TextView accept;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
卸下支架,然後用 – Rohit
只爲「接受」的TextView或所有? –
@Rohit爲什麼ViewHolder有問題? – Raghunandan