hi i have a list view that contains list of elements i want to use onitemclick method to open another list

StackOverflow https://stackoverflow.com/questions/23339251

  •  10-07-2023
  •  | 
  •  

Question

hi i have a list view that contains list of methods so here i am going to display it's parameters in another list so when i click on list item(method) it has to display it's parameter it can achieved through onitemclick() method i tried but it shows some error
can any one help please

this is my listview.java file

public class Listview extends Activity{
private CursorAdapter customAdapter;
private Param databaseHelper;
private static final int ENTER_DATA_REQUEST_CODE = 1;
private ListView listView;     
private static final String TAG = Listview.class.getSimpleName();

private ListView lv;
public static ArrayList<String> your_array_list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    databaseHelper = new Param(this);                       
   customAdapter = new CursorAdapter (this, databaseHelper.getTripList( Param.DESC), 
   0);    
    listView = (ListView) findViewById(R.id.listView1); 
    listView.setAdapter(customAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long    
 id) {
            Log.d(TAG, "clicked on item: " + position);
        }
    });  


    lv = (ListView) findViewById(R.id.listView1);
     Button backButton=(Button)findViewById(R.id.methodbackbutton1);
     backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Listview.this.finish();
            }
        });}

  @Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, your_array_list );
    lv.setAdapter(arrayAdapter); 
     try {
      DisplayM.main();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
@Override
public void onBackPressed()
{
    Intent intent3 = new Intent(this, Nextactivity.class);
    startActivity(intent3);
    finish();
 }
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}




}

this is my listview.xml file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewMyRecording"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/listofMethods"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="wrap_content"
    android:layout_height="384dp" >

</ListView>

 <Button
    android:id="@+id/methodbackbutton1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:onClick="onBackPressed"
    android:text="@string/Backtopreviousscreen" />

</LinearLayout>
Was it helpful?

Solution

You are taking the reference of Same ListView resource findViewById(R.id.listView1) in both variable listView & lv.

Second thing, why you are taking Cursor Adapter for your list?

Third thing, what's the purpose of DisplayM.main();

Fourth thing, you can finish an activity by writing just finish() then why you are writing it like Listview.this.finish(); ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top