0

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!

3
  • Are you trying to compare booleans to integers? Or Strings to Integers? This is Java, so you need to be comparing things of the same type. Commented Mar 4, 2015 at 14:29
  • NO, I don't ask about comparing - I want to set text to elements in listview (elements from db with values 1/0)and i want to show this in listview like synchronized/not synchronized Commented Mar 4, 2015 at 14:33
  • OK, have you tried what you're showing as an example? Is there no documentation for this Android class? It also seems like you're trying to compare the string value "0" with the integer value 0, which may not be the same thing in Android. You see what I'm saying? Either make note that you're using pseudocode or try to make your example at least believably work. Commented Mar 4, 2015 at 14:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to check if your string is set to "0" in the resource than try this in your activity getResources().getString(R.string.is_synchronized).equals("0")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.