简体   繁体   中英

Android Listview - get and update element of element

i need help with android listview. I have a simple list like this:

        ArrayList<HashMap<String, String>> tracksList =  controller.getAllTracksInfo();
    //
    if(tracksList.size()!=0){
        //Set the User Array list in ListView
        ListAdapter adapter = new SimpleAdapter( checkDB.this,tracksList, R.layout.simplerow, new String[] { "Title","Synchronized"}, new int[] {R.id.track_title, R.id.is_synchronized});
        myList=(ListView)findViewById(R.id.tracksinfo_list);
        myList.setAdapter(adapter);

But in element "R.id.is_synchronized" I want to make something like:

if (R.id.is_synchronized.getText().equals(0)) {
R.id.is_synchronized.setText(synchronized)
}

How can I do this?

Many thanks!

I always create a BaseAdapter for my ListViews. So step by step how to create your own adapter and get it working.

public class MyCustomAdapter extends BaseAdaper {

    private Activity activity;
    private ArrayList<HashMap<String, String>> tracksList;
    private static LayoutInflater inflater = null;

    public MyCustomAdapter(Activity activity, ArrayList<HashMap<String, String>> tracksList) {
        this.activity = activity;
        this.trackList = trackList;  
        this.inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
    }

    @Override
    public int getCount() {
        return trackList.size();
    }

    @Override
    public Object getItem(int position) {
        return trackList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        TextView title;
        TextView isSynchronized;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.mycustomlayout, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView
                .findViewById(R.id.track_title);
            holder.isSynchronized = (TextView) convertView
                .findViewById(R.id.is_synchronized);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(tracksList.getTitle());
        holder.isSynchronized.setText(tracksList.isSynchronized());
        //you can do here what ever you want with the textviews like check if its == 0 etc
    }
}

How to use it:

mylistView = (ListView) findViewById(R.id.);
adapter = new MyCustomAdapter(Activity.this,
            tracksList);
mylistView.setAdapter(adapter);

For more info search for custom list adapter. Why use a list adapter? Well you have more control of what is shown, how, when. If you need your list to be more complex it's not a problem. You want to create a nice background with buttons, different colors no problem just create the mycustomlayout.xml add add what you want there and how you want it.

PS - the above is only an example it's not a 100% working code, you'll need to set the tracklist and get the data you need, create the mycustomlayout.xml.

如果要检查资源中的字符串是否设置为“ 0”,请在活动中尝试此操作getResources().getString(R.string.is_synchronized).equals("0")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM