user1903270
user1903270

Reputation: 77

how set the text of textview with listitem when the listitem is clicked

I want to set the text of textview with listitem when a list item is clicked. i have list of items. when i click on an item it displays the html page. i have layout to display the HTML page(Using WebView), heading(TextView) and back button(Imagebutton).i am able to display the html page in webview without any problem. Problem is with the heading. when i click on list item of list. i want the same item to be set as heading in the textview. Please suggest me. i am able to toast the listitem, but i do not need it. i want to set in text of textview.please see the below code.

Topics.java

public class Topics extends ListActivity {

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

          // storing string resources into Array
          String[] topic_sections =   getResources().getStringArray(R.array.topic_sections);

    // Binding resources Array to ListAdapter
         this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, topic_sections));
}     

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    change(position);
    }
void change(int position){
   switch(position){  
            case 0 :{
        Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
        intent.setData(Uri.parse("file:///android_asset/home.html"));
        startActivity(intent);
            }
           break;

    case 1 :{
        Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
        intent.setData(Uri.parse("file:///android_asset/program.html"));
        startActivity(intent);}
           break;

       } } }

TopicsDisplay.java

public class TopicsDisplay extends Activity {


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


WebView webview1 = (WebView) findViewById(R.id.webView1);
webview1.loadUrl(getIntent().getDataString());
webview1.getSettings().setBuiltInZoomControls(true);
webview1.setInitialScale(1);
webview1.setPadding(0, 0, 0, 0);




 }

 public void finishActivity(View v){
 finish();
  }

  }

list_item.xml

 <?xml version="1.0" encoding="utf-8"?>
 <!--  Single List Item Design -->
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/label"
      android:textColor="#ea9999"
      android:background="#000000"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10dp"
      android:textSize="20sp"
      android:textStyle="bold" 
      android:focusable="false"  
      android:clickable="false">
 </TextView>

topic_display.xml

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

 <RelativeLayout 

    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >


    <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="42dp"
    android:layout_height="42dp"
    android:onClick="finishActivity"
    android:contentDescription="@string/back"
    android:src="@drawable/back_button" />

    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="45dp" 
    android:textStyle="bold"
    android:textColor="#000000"
    android:gravity="center_vertical"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:paddingLeft="10dp"
    android:id="@+id/header"

    />

 </RelativeLayout>

 <WebView 

    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/webView1"/>


 </LinearLayout>

After getting the item from the list view how to set that listitem in the text of textview which is there in the topic_display layout.

Upvotes: 1

Views: 985

Answers (1)

petlack
petlack

Reputation: 216

My suggestion is to put the title you want to show in the Intent's extras. It means, that when you call

    Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
    intent.setData(Uri.parse("file:///android_asset/home.html"));
    startActivity(intent);

you should put the title in the Intent's extra before calling the startActivity() method (in both cases you are calling the TopicsDisplay activity)

    Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
    intent.setData(Uri.parse("file:///android_asset/home.html"));
    intent.putExtra("title", topic_sections[position]);
    startActivity(intent);

And then, in the TopicsDisplay activity you can get the title by calling

getIntent().getStringExtra("title")

Finally, you should end up with something like this in the TopicsDisplay.onCreate method:

TextView titleTV = (TextView) findViewById(R.id.header)
titleTV.setText(getIntent().getStringExtra("title"));

Hope this helps!

Upvotes: 2

Related Questions