2016-04-08 41 views
0

我嘗試列表。在這份名單中的每一行都會有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); 

回答

0
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) { 

//Your only mistake was View view = convertView; your list was removing the item but because of this line your view was not refreshing 
    View view = null; 
    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; 
} 
} 
+0

喔...謝謝!但是這並沒有對delate進行任何處理,並且在添加上有所增加) –

+0

對不起,我希望我編輯的答案能幫助你。 – Drv

+0

)我會嘗試它...但我找不到與我的一個樣本有差異... –

0

我也做了類似@drv,使用標籤,但它是更優雅的使用列表視圖本身,如果你問我。這裏是我自己的代碼(我剛剛寫了幾天前)使用gridview的一個例子,但它基本上是一樣的東西(listview有基本的方法)。在我的情況下,我正在使用一個片段,所以我通過自定義回調傳遞它。

mGridView = (GridView) rootView.findViewById(R.id.grid_view); 
... 
    mAdapter = new ImageWithTitleAdapter(getContext(), new ArrayList<Movie>()); 
    mGridView.setAdapter(mAdapter); 

    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      ((Callback) getActivity()) 
        .onItemSelected(v, position); 
     } 
    }); 

後來在片段:

/** 
* A callback interface that all activities containing this fragment must 
* implement. This mechanism allows activities to be notified of item 
* selections. 
*/ 
public interface Callback { 
    /** 
    * DetailFragmentCallback for when an item has been selected. 
    */ 
    void onItemSelected(View view, int pos); 
} 
在活動

然後,執行回調:

public class MainActivity extends AppCompatActivity implements MainFragment.Callback { 
    /** 
* MainFragment Callback override - handles click events on gridview items 
* @param v - the calling view containing the data item 
* @param pos - the position of the view from the grid_view 
*/ 
@Override 
public void onItemSelected(View v, int pos) { 
    Movie movie = (Movie) v.getTag(); 
    // We are splitting the logic here because we don't just need to load the detail fragment 
    // with the data. We also need to completely switch activities if it is single-pane 
    if (LayoutUtil.twoPanes(this)) { 
     loadDetailInTwoPane(pos, movie); 
    } else { 
     Intent intent = new Intent(this, DetailActivity.class) 
       .putExtra(getString(R.string.key_grid_view_movie), movie) 
       .putExtra(getString(R.string.key_grid_view_position), pos); 
     startActivity(intent); 
    } 
} 
+0

謝謝......但它看起來像「一些魔法」......但我保證生病試圖理解它!)) –

相關問題