我嘗試列表。在這份名單中的每一行都會有3種狀態:如何在自定義列表適配器中獲取位置ID?
1)使用文本和按鈕GET」
2)上載有進度條和按鈕狀態取消
3)就緒狀態使用文本和按鈕啓動狀態上傳
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
// private ArrayList<String> list2 = new ArrayList<String>();
private Context context;
private int[] laytypes;
//private String[] list;
public MyCustomAdapter(ArrayList<String> list,int[] laytypes,Context context) {
this.list = list;
//this.list2 = list2;
this.context = context;
this.laytypes=laytypes;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_firmware, null);
TextView listItemText = (TextView)view.findViewById(R.id.list_item_st1);
listItemText.setText(list.get(position));
/*
if(laytypes[position]==1)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_st1, null);
ProgressBar DownloadBar;
DownloadBar = (ProgressBar)view.findViewById(R.id.downloadProgressBar);
DownloadBar.setProgress(50 + laytypes[position]);
DownloadBar.setMax(100);
}
else if(laytypes[position]==2)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_st2, null);
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
}
else
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_st3, null);
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
}
*/
}
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setTag(position);
Button addBtn = (Button)view.findViewById(R.id.add_btn);
addBtn.setTag(position);
deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
laytypes[position]=1;
//list2.remove(position);
notifyDataSetChanged();
}
});
return view;
}
}
但例如在推刪除按鈕 - 最後一行刪除,而不是當前(點擊行)。如何獲得當前行ID?
在我的活動我有這樣的代碼:
//TEST CUSTOM LIST CREATION
//generate list
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");
list.add("item4");
list.add("item5");
list.add("item6");
int[] laytypes = new int[50];
laytypes[0]=0;
laytypes[1]=0;
laytypes[2]=0;
laytypes[3]=0;
laytypes[4]=0;
laytypes[5]=0;
laytypes[6]=0;
//instantiate custom adapter
MyCustomAdapter adapter = new MyCustomAdapter(list,laytypes,this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.listViewTest);
lView.setAdapter(adapter);
喔...謝謝!但是這並沒有對delate進行任何處理,並且在添加上有所增加) –
對不起,我希望我編輯的答案能幫助你。 – Drv
)我會嘗試它...但我找不到與我的一個樣本有差異... –