2014-03-14 39 views
0
中的文本

我正在显示来自数据库的事件名称,并将它们添加到Layout。现在,我想打开另一个布局,其中包含我单击的特定事件的详细信息。如何我将onclick添加到我已显示的文本中?我想让文字可点击。如何将onclick添加到android

package com.example.desisquarea; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.Bundle; 
import android.app.ListActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class FullDetails extends ListActivity { 
    //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS 
    ArrayList<String> listItems=new ArrayList<String>(); 

    //DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW 
    ArrayAdapter<String> adapter; 
    private ListView lv; 
    //RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED 
    int clickCounter=0; 
    @Override 
    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.activity_full_details); 
     adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems); 
     setListAdapter(adapter); 
     Log.v("hiiii", "[email protected]@@@ "); 
     lv = (ListView) findViewById(android.R.layout.listView1); 
     Log.v("hiiii", "[email protected]@@@ "); 
     fullItems(null); 


    } 
    //ArrayList<String> all = new ArrayList<String>(); 
    JSONArray all=new JSONArray(); 
    String size; 
    //METHOD WHICH WILL HANDLE DYNAMIC INSERTION 
    public void fullItems(View v) { 
     String result = ""; 
     InputStream is=null; 
     //the year data to send 
    // ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     // nameValuePairs.add(new BasicNameValuePair("year","1980")); 

     //http post 
     try{ 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://motal.com/and/events.jsp"); 
       HttpResponse response = httpclient.execute(httppost); 
         HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 
     //convert response to string 
     try{ 
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 

       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close(); 

       result=sb.toString(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     //parse json data 
     try{ 
       JSONArray jArray = new JSONArray(result); 
          for(int i=0;i<jArray.length();i++){ 

         JSONObject json_data = jArray.getJSONObject(i); 

        size=json_data.getString("size"); 

        for(int j=0;j<Integer.parseInt(size);j++){ 

         // Log.v("JSONObject", "JSONObject @@@"+json_data.getJSONArray("res"+j)); 
         all=json_data.getJSONArray("res"+j); 
        listItems.add("ID"+all.get(0)); 
        listItems.add("\n Venue"+all.get(1)); 
        listItems.add(" Price:"+all.get(2)); 

         //listItems.add(all.get(0)+" \n venue:"+all.get(1)+" Price:"+all.get(3)); 
        lv.setAdapter(adapter); 
        } 

       } 

     } 
     catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 





     adapter.notifyDataSetChanged(); 
    } 
} 

activity_full_details.xml

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

     <ListView 
     android:id="@android:id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:drawSelectorOnTop="false" /> 

</LinearLayout> 

日志消息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.desisquarea/com.example.desisquarea.FullDetails}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
+2

您可以设置一个监听TextView的。 – Raghunandan

+0

阅读本文[this](http://stackoverflow.com/questions/6226699/how-to-make-text-view-clickable-in-android)previous question.Explains几乎所有你需要知道的关于制作短信在Android中可点击 –

+0

你想要什么?那是ListView还是TextView? – anuruddhika

回答

1
listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Toast.makeText(getApplicationContext(), 
     "Click ListItem Number " + position, Toast.LENGTH_LONG) 
     .show(); 
    } 
}); 
+0

感谢您的回复。在上面我只添加文本layout.how我可以将文本转换为文本视图? – PRK449

0

设置onclickListenerTextView

就像这样: -

TextView = textView (TextView) findViewById(R.id.TEXTVIEW_ID); 
textview.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 
0

您可以设置的ArrayList到ListView的这个样子。

private ListView lv; 

在onCreate方法

lv = (ListView) findViewById(R.id.listView1); 

变化这一行listItems.add(all.get(0)+" \n venue:"+all.get(1)+" Price:"+all.get(3));这样。

listItems.add("ID"+all.get(0)); 
listItems.add("Venue"+all.get(1)); 
......................... etc 

然后添加到addapter

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
    this, 
    android.R.layout.simple_list_item_1, 
    listItems); 

    lv.setAdapter(arrayAdapter); 

之后,你可以set OnItemClickListner这样

lv.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { 
      // Put your custom code here 
     } 
}); 

添加这activity_full_details.xml

<ListView 
     android:id="@+id/listView1" 
     android:layout_width="288dp" 
     android:layout_height="330dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="62dp" > 
</ListView> 
+0

感谢您的回复。在上面我只添加文本layout.how我可以将文本转换为文本视图? – PRK449

+0

检查我的更新 – anuruddhika

+0

在上面我添加文本最初到'ArrayList',然后添加到布局不能添加此'text.setText(“您的文本”)''同时添加到'arrylist' – PRK449

1

它很简单,这个:

txtView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
    } 
}); 

希望它有帮助。

+0

感谢您的回复。在上面我最初将文本添加到ArrayList,然后添加到布局不能将文本转换为'textview',同时添加到'arraylist' – PRK449

1

这是很简单的,那样做:

TextView textView = (TextView) findViewById(R.id.yourtextview); 
textView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // Codes for action 
     } 
    }); 

更新 Converting string into TextView?

TextView idText = new TextView(this); 
    idText.setText(row.get(0).toString()); 
    tableRow.addView(idText); 

    TextView storeText = new TextView(this); 
    storeText.setText(row.get(1).toString()); 
    tableRow.addView(storeText); 

    TextView maggiText = new TextView(this); 
    maggiText.setText(row.get(2).toString()); 
    tableRow.addView(maggiText); 
+0

感谢您的回复。在上面我只添加文本layout.how我可以将文本转换为文本视图? – PRK449