Search code examples
androidstringandroid-intenttextviewsettext

Change TextView with Intent (String)


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);
    }

    }
}

Solution

  • 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.