我有一个列表,我想在列表中的每一行的单选按钮上给出点击事件。但是这段代码不起作用。强行关闭。请允许我帮忙。我是新手。
package com.example.radiogroup;
import java.util.ArrayList;
import java.util.HashMap;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements Checkable
{
private ArrayList<HashMap<String,Object>> data;
private ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
data=new ArrayList<HashMap<String, Object>>();
prepareListData();
adapter = new ListAdapter(this,data);
lv.setAdapter(adapter);
/* lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
{
if(position==0)
{
Toast.makeText(getApplicationContext(), "Move forward",
Toast.LENGTH_SHORT).show();
}
}
}
}); */
}
private void prepareListData()
{
HashMap<String,Object> row1=new HashMap<String,Object>();
row1.put("ID",1 );
row1.put("NAME","Taj Mahal");
data.add(row1);
HashMap<String,Object> row2=new HashMap<String,Object>();
row2.put("ID",2 );
row2.put("NAME","Agra Fort");
data.add(row2);
HashMap<String,Object> row3=new HashMap<String,Object>();
row3.put("ID",3 );
row3.put("NAME","Ram Baugh");
data.add(row3);
HashMap<String,Object> row4=new HashMap<String,Object>();
row4.put("ID",4 );
row4.put("NAME","Panch Mahal");
data.add(row4);
HashMap<String,Object> row5=new HashMap<String,Object>();
row5.put("ID",5 );
row5.put("NAME","Musamman Burj");
data.add(row5);
}
class ListAdapter extends BaseAdapter
{
private Context context;
private ArrayList<HashMap<String,Object>> list;
ViewHolder holder;
public ListAdapter(Context context,ArrayList<HashMap<String,Object>> list){
this.context = context;
this.list = list;
holder.rb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Move forward", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row,null,false);
holder.rb = (RadioButton) convertView.findViewById(R.id.radioButton1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.rb.setText(list.get(position).get("NAME").toString());
return convertView;
}
class ViewHolder
{
RadioButton rb;
}
}
@Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
}
@Override
public boolean isChecked() {
// TODO Auto-generated method stub
return false;
}
@Override
public void toggle() {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
在适配器中的getview方法中使用它
holder.rb.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Move forward", Toast.LENGTH_SHORT).show();
}
});
在OnCreate Methed上使用它
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
{
if(position==0)
{
Toast.makeText(getApplicationContext(), "Move forward "+position, Toast.LENGTH_SHORT).show();
}
else
if(position==1)
{
Toast.makeText(getApplicationContext(), "Move forward "+position,
Toast.LENGTH_SHORT).show();
}
}
}
});
答案 1 :(得分:0)
在 ListAdapter 类的构造函数中,您尚未初始化ViewHolder对象,因此您无法访问该类的成员。 删除
holder.rb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Move forward", Toast.LENGTH_SHORT).show();
}
});
来自 ListAdapter 构造函数,并在类和成员初始化之后将其放在 getView()中。
后
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row,null,false);
holder.rb = (RadioButton) convertView.findViewById(R.id.radioButton1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
在getView方法中。