没有普通ID的listview上的onclick监听器

时间:2014-12-02 20:57:39

标签: android listview

我有这个代码用于创建自定义listadapter 这是我的代码的一部分:

protected void onPostExecute(String success)
{
    ListAdapter adapter = new SimpleAdapter (context, jsonList,
            R.layout.items, new String[]{TAG_PELI, TAG_nombre2},
            new int[]{R.id.nombrePeli, R.id.nombreTwo});
    setListAdapter(adapter);
    getListView();
}
在我的xml中使用自定义列表视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android:id="@android:id/list"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true" >

</ListView>

问题在于电话&#34; onclicklistener&#34; id未定义,因为声明了 android:id =&#34; @android:id / list&#34;,我必须选择此表单,因为抛出错误。我有什么要打电话&#34; oncliclistener&#34;。谢谢

编辑===============================(已解决)

这段代码对我有用! :)谢谢

public class MainActivity extends Activity {
private Context context;
private static String url ="http://comupunt.esy.es/cities.php";
private static final String TAG_PELI = "name";
private static final String TAG_nombre2 = "nametwo";

ListView list;

ArrayList<HashMap<String, String>> jsonList = new ArrayList<HashMap<String,String>>();

protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =(ListView) findViewById(R.id.list);
new GetJSONActivity(MainActivity.this).execute();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    HashMap<String,String> map =  
   (HashMap<String,String>)list.getItemAtPosition(position);
    String value = map.get(TAG_PELI);
    String products = map.get(TAG_nombre2);
    Toast toast1 =
            Toast.makeText(getApplicationContext(),
                   value, Toast.LENGTH_SHORT);

        toast1.show();
}
});
}

public boolean onCreateOptionsMenu(Menu menu)
{

getMenuInflater().inflate(R.menu.main, menu);

return true;
}

private class GetJSONActivity extends AsyncTask<String, Void, String>
{
private Activity activity;
public GetJSONActivity(Activity activity)
{
this.activity = activity;
context = activity;
}
@Override
protected String doInBackground(final String... args) {
    // TODO Auto-generated method stub
    JSONParser jparser = new JSONParser();
    try{
        JSONArray json = jparser.GetJSONfromUrl(url);
        for(int i =0;i <json.length();i++){
            try {
                JSONObject c = json.getJSONObject(i);
                String vpeli = c.getString(TAG_PELI);
                String vname2 = c.getString(TAG_nombre2);
                HashMap <String, String> map = new HashMap<String,String>();
                map.put(TAG_PELI, vpeli);
                map.put(TAG_nombre2, vname2);
                jsonList.add(map);
            } catch (JSONException e) {
                // TODO: handle exception
                e.printStackTrace();
                return "error creando variables";
            }
        }
    ;   }catch (Exception e)
    {

    }
    return "exito";
 }
 protected void onPostExecute(String success)
{
ListAdapter adapter = new SimpleAdapter (context, jsonList,
        R.layout.items, new String[]{TAG_PELI, TAG_nombre2},
        new int[]{R.id.nombrePeli, R.id.nombreTwo});
list.setAdapter(adapter);   

}


}
}

2 个答案:

答案 0 :(得分:0)

在您的XML中使用您自己的应用专用ID,使用&#34;添加到&#34;语法@+id

android:id="@+id/list"

然后,您可以在代码中将其引用为R.id.list

===编辑

好的,我看到你现在正在做什么,虽然你并没有真正联系到一个听众。你应该能够做到这一点:

getListView().setOnClickListener(this);

然后,您的Activity只是恰当地实施onClick()方法(尽管我不确定您是否真的想在这里做什么。)

答案 1 :(得分:0)

ListView文件中的xml分配一个ID:

<ListView
    android:id="@+id/my_list_id"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true" >

</ListView>

并在你的java代码中:

public class MainActivity extends Activity {
ListView list;
private Context context;
private static String url ="http://comupunt.esy.es/cities.php";
private static final String TAG_PELI = "name";
private static final String TAG_nombre2 = "nametwo";

ListView listView;

ArrayList<HashMap<String, String>> jsonList = new ArrayList<HashMap<String,String>>();

protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.my_list_view);
new GetJSONActivity(MainActivity.this).execute();
}

public boolean onCreateOptionsMenu(Menu menu)
{

    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

private class GetJSONActivity extends AsyncTask<String, Void, String>
{
private Activity activity;
public GetJSONActivity(Activity activity)
{
this.activity = activity;
context = activity;
}
    @Override
    protected String doInBackground(final String... args) {
        // TODO Auto-generated method stub
        JSONParser jparser = new JSONParser();
        try{
            JSONArray json = jparser.GetJSONfromUrl(url);
            for(int i =0;i <json.length();i++){
                try {
                    JSONObject c = json.getJSONObject(i);
                    String vpeli = c.getString(TAG_PELI);
                    String vname2 = c.getString(TAG_nombre2);
                    HashMap <String, String> map = new HashMap<String,String>();
                    map.put(TAG_PELI, vpeli);
                    map.put(TAG_nombre2, vname2);
                    jsonList.add(map);
                } catch (JSONException e) {
                    // TODO: handle exception
                    e.printStackTrace();
                    return "error creando variables";
                }
            }
        ;   }catch (Exception e)
        {

        }
        return "exito";
}
protected void onPostExecute(String success)
{
    ListAdapter adapter = new SimpleAdapter (context, jsonList,
            R.layout.items, new String[]{TAG_PELI, TAG_nombre2},
            new int[]{R.id.nombrePeli, R.id.nombreTwo});
    listView.setAdapter(adapter);
    notifyDataSetChanged();

}

}
}

现在您的列表中包含有效ID,您可以像这样使用它:R.id.my_list_id