0

I am creating my very first Android application, but i stuck unfortunately. The application would be very simple: On the starting page there is a ListView with items like: 1st group 2nd group 3rd group ...

By clicking on any of these items a new page would show up with a single textview element that would have some description. Like you click on '1st group' item, the listview gets hidden, and a new page appears with '1st group description' text.

So far I can show the listview with the items, but when I click on them, nothing happens (i guess I miss some basic stuff, but as a very newby, i cannot find it out easily).

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.view.*;

public class SimpleListViewActivity extends Activity {
    LinearLayout.LayoutParams layoutParams;
    LinearLayout ll;


  private ListView mainListView ;
  private ArrayAdapter<String> listAdapter ;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.mainListView );

    // populate the List of groups
    String[] GROUP = getResources().getStringArray(R.array.group);  
    ArrayList<String> GrList = new ArrayList<String>();
    GrList.addAll( Arrays.asList(GROUP) );

    // Create ArrayAdapter using the list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, GrList);

    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter );  
    ll = (LinearLayout)findViewById(R.id.LinearLayout);
    layoutParams = new LinearLayout.LayoutParams
    (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    mainListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView t = (TextView) findViewById(R.id.textView1);
            String[] DESC = getResources().getStringArray(R.array.desc); 
            t.setText(DESC[position]);
            ll.addView(t);
        //This is the point that is wrong for sure (and others maybe also). I cannot get the textview shown
        }

    });

  }

}

Thanks for your help.

skandigraun
  • 741
  • 8
  • 21
  • Is your `TextView` visible? Post your `XML` file. – slybloty Aug 23 '12 at 18:30
  • Whats up with the layoutParams you are creating, they are not used in the code you posted, and you should leave layoutparams within the xml you are retrieving your views from. – josh527 Aug 23 '12 at 18:44

3 Answers3

0

Have you tried displaying a toast message or setting a breakpoint within your onItemClick() method to verify that its not being reached? My guess is that it is and you are running into one of the issues described here:

Refreshing a LinearLayout after adding a view

Community
  • 1
  • 1
Nick
  • 8,181
  • 4
  • 38
  • 63
0

I am assuming your R.layout.main is holding a listview and a linear layout with ids R.id.mainListView, and R.id.LinearLayout respectively.

Example: I left out some of the obvious attributes you would need like height width etc..

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      >
      <ListView android:id="@+id/mainListView" />
      <LinearLayout android:id="@+id/LinearLayout" />
 </RelativeLayout>

In your on item click, all you will do is add a textview as you have done, then set the mainListView.setVisibility(View.INVISIBLE) and ll.setVisibility(View.VISIBLE).

If your R.layout.main is not using a RelativeLayout as the root node, but is instead using a LinearLayout you should still be able to achieve the same effect by setting the Visibilities to View.GONE if you want it to hide, and View.VISIBLE if you want it to show.

To revert back to being able to see the list view I would override onBackPressed() in the activity, to invert the Visibilities on the two items. Also remember to remove all views from the linear layout so that the next time an item in the group is selected it will be the only item in the linear layout when it is added.

There are much easier ways to accomplish this, such as firing off a new activity for viewing the next item, but seems you are keeping everything within one activity. I would also think about using a ListActivity instead of base activity class.

Hope this helps.

josh527
  • 6,971
  • 1
  • 19
  • 19
0

First off stop using the word page. Call it an activity (gotta get you in the Android zone)

Once the click happens start a new activity like so:

mainListView.setOnItemClickListener(new OnItemClickListener() {

String textToPass = GrList.get(position)
Intent i = new Intent(getBaseContext(), SecondActivity.class);                      
i.putExtra("textToPass", textToPass);
startActivity(i);

}

You'll obviously need to have that second activity with its corresponding layout file defined. Also in the second activity look up how to get the bundle and extras from the first activity in order to get the textToPass String

MobileMon
  • 8,341
  • 5
  • 56
  • 75