Jed
Jed

Reputation: 1043

Can't see text view value inlistview android

This is my code, I can't see the value of the textview in my listview.

My toast doesn't work

I am a newbie to Android why can't I see it:

I can't get the value of

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adpt  = new SimpleAdapter(new ArrayList<Contact>(), this);
    ListView lView = (ListView) findViewById(R.id.listview_search);

    lView.setAdapter(adpt);
    lView.setTextFilterEnabled(true);

    String[] values = new String[] {
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z"
    };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);


    // Assign adapter to ListView
    lView.setAdapter(adapter);

    // React to user clicks on item
    lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            TextView firstletter = (TextView) view.findViewById(R.id.firstletter);
            Toast.makeText(MainActivity.this, firstletter.getText(),

  Toast.LENGTH_SHORT).show();
            //Intent i = new Intent(getApplicationContext(), ContactsActivity.class);
            //i.putExtra("firstletter", name.getText() );
            //i.putExtra("email", email.getText());
            //startActivityForResult(i, 100);

        }
    });
}

This is my list_item in my listview in activity_main layout:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:background="#ff9ac2ff"
                 android:padding="8dp">
    <LinearLayout android:id="@+id/item_layout"
                  android:orientation="vertical"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:paddingLeft="10dip">
        <TextView
                android:id="@+id/firstletter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FF7F3300"
                android:textSize="20dip"
                android:textStyle="italic"
                />
    </LinearLayout>
</RelativeLayout >

Upvotes: 0

Views: 368

Answers (1)

linakis
linakis

Reputation: 1231

I can't understand exactly what you are trying to do with adpt = new SimpleAdapter(new ArrayList<Contact>(), this); and why you assign this adapter to your ListView lView only to re-assign it with ArrayAdapter<String> adapter. I guess you were trying to make a point and forgot some bits and pieces.

Your major problem is the way you are trying to get the value of the clicked list item with:

TextView firstletter = (TextView) view.findViewById(R.id.firstletter);
Toast.makeText(MainActivity.this, firstletter.getText(), Toast.LENGTH_SHORT).show();

what you should do, since you already know the values (you are the one who assigned them in the first place :P), is simply:

Toast.makeText(MainActivity.this, values[position],Toast.LENGTH_SHORT).show();

add a final to your String[] values and you are good to go.

Upvotes: 1

Related Questions