-2

i want to open new activity as i click at my list item. here is my code for main java class. please help me. MainActivity.java

public class MainActivity extends Activity {
private ListView myLV;
private String[] stringArray;
ArrayAdapter<String> myAdapter;
public Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    stringArray = new String[10];
    for(int i=0; i<10; i++)
    {
        stringArray[i] = "String" + i;

    }

    myLV = (ListView)findViewById(R.id.ListView);

    MyAdapter adp = new MyAdapter(this, stringArray);
    myLV.setAdapter(adp);
}


private class MyAdapter extends ArrayAdapter<Object> {
    private LayoutInflater inflater;

    public MyAdapter(Activity activity, String[] items){
        super(activity, R.layout.row_tweet, items);
        inflater = activity.getWindow().getLayoutInflater();
    }
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null)
        {
            convertView=inflater.inflate(R.layout.row_tweet, parent, false);
        }

        return convertView;
    }
    @Override
    public int getCount(){
        return 10;
        }

    }
}

if i change public class MainActivity extends Activity to ListActivity then my application crashes and show unfortunately stopped working error. I really don't know how to fix it.

MWiesner
  • 8,868
  • 11
  • 36
  • 70

3 Answers3

0

If I'm getting you that's what you want:

convertView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick( View view ) {
        Intent newActivityIntent = new Intent( MainActivity.this, OtherActivity.class );
        startActivity(newActivityIntent); 
    }
}); 

Hope it helps.

Nicolas Cortell
  • 659
  • 4
  • 16
0

You should use something like this:

public class MainActivity extends Activity {

private ListView myLV;
private String[] stringArray;
ArrayAdapter<String> myAdapter;
public Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringArray = new String[10];
for(int i=0; i<10; i++)
{
    stringArray[i] = "String" + i;

}

myLV = (ListView)findViewById(R.id.ListView);

MyAdapter adp = new MyAdapter(this, stringArray);
myLV.setAdapter(adp);

myLV.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //Your code by position item...
        }
    });
}


private class MyAdapter extends ArrayAdapter<String> {

private Context context;

public MyAdapter(Context context, String[] items){
    super(activity, R.layout.row_tweet, items);
    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.
      getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    return inflater.inflate(R.layout.row_twee, parent, false);
}

@Override
public int getCount(){
    return 10;
}

}
}

Good luck!

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
Jose Angel Maneiro
  • 1,226
  • 9
  • 20
0

If you want to change the layout of a ListActivity you still can with setContentView() method from Activity. As long as there is a ListView called @android:id/list somewhere in your View. The ListActivity will still work.

See here

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256