myo htet aung
myo htet aung

Reputation: 107

getView not rendering even though data shown in LogCat

I am dealing with android ListView and I faced with strange error when i inflating ListItem. I can't see my data on getView method even though datas are already shown in LogCat. Any Ideas ?

private ListViewAdapter m_adapter;
public ArrayList<String> cat = new ArrayList<String>();
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
String title, character, thumbnail, totalPages;
int Code;
ViewHolder vh;
TextView titleView, charView, totalView;
ImageView thumbView, removeView;
Uri thumbUri;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Black_NoTitleBar);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.myputet);
    init_UI();
}

public void init_UI() {
    preferences = getApplicationContext().getSharedPreferences(
            Constants.SHARED_PRED, 0);
    editor = preferences.edit();

    title = preferences.getString("TITLE", "");
    character = preferences.getString("CHAR", "");
    thumbnail = preferences.getString("THUMB", "");
    thumbUri = Uri.parse(thumbnail);
    totalPages = preferences.getString("TOTAL", "");
    Code = preferences.getInt("CODE", 0);

    System.out.println("Getting Title " + title);
    System.out.println("Getting character " + character);
    System.out.println("Getting thumbnail " + thumbnail);
    System.out.println("Getting thumbUri " + thumbUri);
    System.out.println("Getting totalPages " + totalPages);

    Log.v("ArrayList", cat.toString());
    ListView lv = (ListView) findViewById(R.id.listview);

    m_adapter = new ListViewAdapter(this, R.layout.myputet_item, cat);
    lv.setAdapter(m_adapter);
}

private class ListViewAdapter extends ArrayAdapter<String> {
    private ArrayList<String> items;

    public ListViewAdapter(Context context, int textViewResourceId,
            ArrayList<String> items) {
        super(context, textViewResourceId, items);
        this.items = items;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.myputet_item, null);
        }

        Log.v("Title View", title);
        titleView = (TextView) v.findViewById(R.id.titleTxtView);
        charView = (TextView) v.findViewById(R.id.charTxtView);
        totalView = (TextView) v.findViewById(R.id.totalTxtView);
        thumbView = (ImageView) v.findViewById(R.id.charImageView);
        removeView = (ImageView) v.findViewById(R.id.removeListItem);
        if (titleView != null) {
            titleView.setText(title);
            Log.v("Update Title", title);
        }
        charView.setText(character);
        Log.v("Update character", character);
        totalView.setText(totalPages);
        Log.v("Update totalPages", totalPages);
        thumbView.setImageURI(thumbUri);

        return convertView;
    }
}

LogCat Output

10-31 23:52:34.438: V/Title View(4568): မိခ်ိဳေစ်းကအျပန္
10-31 23:52:34.438: V/Update Title(4568): မိခ်ိဳေစ်းကအျပန္
10-31 23:52:34.438: V/Update character(4568): ကိုေရႊထူး(ျပည္)
10-31 23:52:34.438: V/Update totalPages(4568): 3

Upvotes: 0

Views: 153

Answers (1)

Sam
Sam

Reputation: 86948

You need to return the View that you modified in getView():

return v; // not convertView

On another note, every row in your ListView will be the same... title, character, etc. never change, so they will be displayed over and over.

Last, please watch Android's Romain Guy discuss efficient adapters to help speed up yours.

Upvotes: 2

Related Questions