2012-09-13 42 views
-1

在我的應用程序要在列表中顯示的一些項目,我一直在String數組的所有項目,並通過使用代碼如何從字符串獲取值到textview?在Android的

String[] activities = getResources().getStringArray(R.array.Contents); 

帶來了現在我的任務就是分配這些字符串數組值我TextView

有什麼建議嗎?

+0

所有的值,以一個單一的TextView或在列表中的TextView ?? –

+0

是否要將數組顯示到TextView中? – Lucifer

+0

列表中的TextView,我使用BaseAdapter,但我無法獲得值如果我使用代碼Textview.setText(活動[位置]) – user1667403

回答

0

試試這個

ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1, activities); 
mylistView.setAdapter(aa); 
0

中MainActivity.class

String[] activities = getResources().getStringArray(R.array.Contents); 
List<RowItem> rowItems; 
In onCreate(){ 
super.onCreate(savedInstanceState); 
setContentView(layoutname); 
rowItems = new ArrayList<RowItem>(); 
for (int i = 0; i < titles.length; i++) { 
RowItem item = new RowItem(activities[i]); 
rowItems.add(item); 
} 
listView = (ListView) findViewById(id of ListView); 
CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems); 
listView.setAdapter(adapter); 
} 

中RowItem類

public class RowItem { 
private String activities; 
public RowItem(String activities { 
this.activities = activities; 
} 
public String getActivities() { 
return activities; 
} 
public void setactivities(String activities) { 
this.activities = activities; 
} 
@Override 
public String toString() { 
return activities; 
} 
} 

在CustomBaseAdapter類

public class CustomBaseAdapter extends BaseAdapter { 
Context context; 
List<RowItem> rowItems; 
public CustomBaseAdapter(Context context, List<RowItem> items) { 
this.context = context; 
this.rowItems = items; 
} 

private class ViewHolder { 
TextView txtActivities; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     LayoutInflater mInflater = (LayoutInflater) 
      context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.list_item, null); 
      holder = new ViewHolder(); 

      holder.txtTitle = (TextView) convertView.findViewById(R.id.activities); 

      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     RowItem rowItem = (RowItem) getItem(position); 


     holder.txtActivites.setText(rowItem.getActivity()); 


     return convertView; 
    } 
} 
相關問題