-1

I have a listview class.listview opening and a user can click any listview item. I wanna set the text on a xml. I have strings like st1-st2-st3. If I clicked any listview item, how can I get it to open the xml and set Textview1 for st1/@String ?

my listview working like this ;

public class strateji extends Activity {
private static final String LOG_TAG = "Alert Dialog Activity";
private ListView lv1;

  private TextView status;
 private String lv_arr[]={"listview1-list2-list3-list4-list5-list6-etc..."};

         /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ana);

        // Look up the AdView as a resource and load a request.

        Log.i(LOG_TAG, "Alert Dialog Activity Started");
        lv1=(ListView)findViewById(R.id.listView1);

        // By using setAdpater method in listview we an add string array in list.

        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));


        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

    if(position == 0)
    {

            setContentView(R.layout.texts); //--->opening texts.xml and setText
        status.setText(String);--------->>> like this i want send a string for TextView



    }
    if(position == 1)
    {
             Intent myIntent =  new Intent(strateji.this, mat2.class);
                 startActivityForResult(myIntent, 0);
    }

    }
}
MacSTD
  • 7
  • 1
  • 9

2 Answers2

0

Ok this is getting a bit confusing so let me just do this.

If you have a layout like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:packpurchase="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_view_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="17sp"
        android:padding="15dp" />

    <View
        android:id="@+id/divider_one"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <ListView
        android:id="@+id/list"
        android:layout_height="match_parent"
        android:layout_width="match_parent" 
        android:divider="@drawable/list_divider_inset"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>

In your activity, create the variables in order to have a reference to your views and adapter:

private ListView lv;
private ArrayAdapter<String> mAdapter;
private TextView tv;

And then in onCreate() once you are done calling super.onCreate() and setContentView(), set your adapter to your ListView while getting your references

lv = (ListView) findViewById(android.R.id.list);
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
lv1.setAdapter(mAdapter);
tv = (TextView) findViewById(R.id.your_textview_id);

lv1.setOnItemClickListener(new OnitemClickListener(){

     @Override
     public void onItemClicked(...){
             // GET YOUR STRING
             String mString = mAdapter.getItem(position);

             // APPLY YOUR STRING TO YOUR TEXT_VIEW
             tv.setText(mString);  
     }
};

Again this is poorly explained and I am not sure if it is correct to what you need. It is just to get you started (or at least to help us understand better what you need).

Note that you don't have to keep a reference to your adapter as you can just call

lv1.getAdapter().getItem(position);

I just keep a reference to it and often see a reference to the adapter kept. Same with TextView tv. You don't have to keep a reference, but you can and once you have that reference you can do null checks to avoid doing to many findViewById() calls which can get heavy.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • First of all thanks for your answer.Where will I write string ? – MacSTD Oct 01 '14 at 20:16
  • @MacSTD check my edits. again not sure if I am doing what you want but that is how you get a string based on what item you click on in the list. – zgc7009 Oct 01 '14 at 20:21
  • Just to clarify, you don't actually want to change your activity when you click right? Just your TextView? – zgc7009 Oct 01 '14 at 20:23
  • am i open a new activity or am i write on down at position = 0 ? – MacSTD Oct 01 '14 at 20:27
  • @MacSTD check my edited answer. Don't think you need to be starting new activities. Or is there a reason for the new activities? – zgc7009 Oct 01 '14 at 20:38
  • @MacSTD I may have some time tonight but I am tied up right now. There are tons of examples on how to do anything I can imagine you are trying to do, just look around for now. – zgc7009 Oct 01 '14 at 20:52
0

If I understand you correctly, you want a new activity to open and a textview in that activity to display text thats dependent on what list view item they clicked?

you would add an intent extra into your intent that you are using to launch the new activity and put the string you want displayed into it, and then in the new activity get that extra and then set the text view to it.

Intent i = new Intent(activity1, activity2);
i.putExtra(stringkey, stringvalue);

then in your oncreate of the activity you call

String str = getIntent.getStringExtra(stringkey);

then just set the textview to that string

Daniel H
  • 389
  • 4
  • 19