2012-09-18 71 views
0

可我們的一些值設置爲每一行就像我們在web編程做Android的列表視圖中添加值

<td id="123">Display</td> 

怎麼能在這個被android.I做使用下面的代碼列表視圖顯示

ArrayList<String> filelist = new ArrayList<String>(); 
final ListView mainlist = (ListView)findViewById(R.id.list); 
.... 
.... 
for (int l=0;l<lines.length;l++) 
{ 
    filelist.addAll(Arrays.asList(lines[l].toString())) ; 
} 
ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_list_item_1, filelist); 
mainlist.setAdapter(adp); 
mainlist.setTextFilterEnabled(true); 
+0

你的意思是說,你要獨特價值附加到每個listItems中的? – Hiral

+0

可能不同或可能是相同的,並且該項目的onclick也會得到該值 – Rajeev

+0

ok ...然後您必須實現自定義適配器類,並且您可以爲其中的每個項目設置標籤! – Hiral

回答

1

你可以試試這個:

CustomAdapter.class:

public class CustomAdapter extends ArrayAdapter<String>{ 

    Context mContext; 
    String[] list; 
    LayoutInflater mInflater; 
    public static HashMap<Integer, Integer> hashkeys=new HashMap<Integer,Integer>(); 

    public CustomAdapter(Context context, int textViewResourceId, 
      String[] objects) { 
     super(context, textViewResourceId, objects); 
     list=objects; 
     mInflater = LayoutInflater.from(context); 
     mContext=context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 

     if(convertView==null) 
     { 
       convertView = mInflater.inflate(R.layout.custom_item, null); 
       holder = new ViewHolder(); 

       holder.tv=(TextView)convertView.findViewById(R.id.textview);     
       convertView.setTag(holder); 
     } 
     else 
     { 
      holder=(ViewHolder)convertView.getTag(); 
     } 
     int value=0; 
     switch(position){ 
      case 0: 
       value=1; 
       break; 
      case 1: 
       value=2; 
       break; 
      case 3: 
       value=3; 
       break;     
     } 
     hashkeys.put(position,value);   

     return convertView; 
    } 
    static class ViewHolder 
    { 
      TextView tv;    
    } 
} 
您的活動

現在,

CustomAdapter adp = new CustomAdapter(this, R.layout.custom_item, filelist); 
mainlist.setAdapter(adp); 
mainlist.setTextFilterEnabled(true); 

mainlist.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, int position, 
         long id) { 

      String unique_value=CustomAdapter.hashkeys.get(position);//get value on click of item 

     } 
}); 
0
private void setList(int viewId, String[] title_list) { 
    ListView listView = (ListView) findViewById(viewId); 
    ArrayAdapter<String> titlesAdaptor = new ArrayAdapter<String>(
      getApplicationContext(), android.R.layout.simple_list_item_1, 
      title_list); 

    listView.setAdapter(titlesAdaptor); 

    listView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, 
       int position, long id) { 
      listOnClicked(view, position, id); 
     } 
    }); 

}// end of setList 

public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{ 
    //handle click events here 
} 
相關問題