Listview 的项目按钮单击
Listview´s items button click
在 Android 上使用 Lisview
。 Listview
的每个项目都有一个 button
,我想在单击每个 button
时打开 activity
。
我的代码是这样的,它可以工作,但我认为有更好的方法来做到这一点。
public class Resul extends Fragment{
ImageView vi;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.resul, container, false);
listview = (ListView) v.findViewById(R.id.lista_resultados_liga);
new DownloadJSON().execute();
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
estado = ((TextView) view.findViewById(R.id.estado))
.getText().toString();
jor = ((TextView) view.findViewById(R.id.jor))
.getText().toString();
vi = ((ImageView) view.findViewById(R.id.imageView1));
vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getActivity(),
Videos.class);
in.putExtra("id_video", resumen_id);
startActivity(in);
}
});
}
}
});
return v;
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
//code
}
@Override
protected Void doInBackground(Void... params) {
//code
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) getActivity().findViewById(R.id.mylist);
// Pass the results into ListViewAdapter.java
adapter = new LazyAdapterResul(getActivity(), arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
我的适配器代码,当我点击图像时我想要它 activity 打开:
public class LazyAdapterResul extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public LazyAdapterResultadosLiga(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public interface MyItemClickListener {
void onItemClick(View view, int position);
}
private MyItemClickListener mItemClickListener;
public void setMyItemClickListener(MyItemClickListener listener) {
this.mItemClickListener = listener;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView equipo_local, equipo_visitante, estado_video;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
jor= (TextView) itemView.findViewById(R.id.jornada);
estado= (TextView) itemView.findViewById(R.id.estado);
vi = (ImageView) itemView.findViewById(R.id.imageView1);
estado.setText(resultp.get(Resul.TAG_ESTADO));
jor.setText(resultp.get(Resul.TAG_JOR));
vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(view, position);
}
}
});
return itemView;
}
}
在你的适配器中尝试这样的接口:
public class ListViewAdapter{
public interface MyItemClickListener {
void onItemClick(View view, int position);
}
private MyItemClickListener mItemClickListener;
public void setMyItemClickListener(MyItemClickListener listener) {
this.mItemClickListener = listener;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Find your image view and add clicklistener
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view, int position) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(view, position);
}
}
});
return convertView;
}
}
现在您可以像这样在 activity 中设置您的 clicklistener:
// Pass the results into ListViewAdapter.java
adapter = new LazyAdapterResultadosLiga(getActivity(), arraylist);
// add click listener
adapter.setMyItemClickListener(new MyItemClickListener(){
@Override
public void onItemClick(View view, int position) {
Intent in = new Intent(getActivity(), MyOtherActivity.class);
startActivity(in);
}
});
// Set the adapter to the ListView
listview.setAdapter(adapter);
在 Android 上使用 Lisview
。 Listview
的每个项目都有一个 button
,我想在单击每个 button
时打开 activity
。
我的代码是这样的,它可以工作,但我认为有更好的方法来做到这一点。
public class Resul extends Fragment{
ImageView vi;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.resul, container, false);
listview = (ListView) v.findViewById(R.id.lista_resultados_liga);
new DownloadJSON().execute();
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
estado = ((TextView) view.findViewById(R.id.estado))
.getText().toString();
jor = ((TextView) view.findViewById(R.id.jor))
.getText().toString();
vi = ((ImageView) view.findViewById(R.id.imageView1));
vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getActivity(),
Videos.class);
in.putExtra("id_video", resumen_id);
startActivity(in);
}
});
}
}
});
return v;
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
//code
}
@Override
protected Void doInBackground(Void... params) {
//code
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) getActivity().findViewById(R.id.mylist);
// Pass the results into ListViewAdapter.java
adapter = new LazyAdapterResul(getActivity(), arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
我的适配器代码,当我点击图像时我想要它 activity 打开:
public class LazyAdapterResul extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public LazyAdapterResultadosLiga(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public interface MyItemClickListener {
void onItemClick(View view, int position);
}
private MyItemClickListener mItemClickListener;
public void setMyItemClickListener(MyItemClickListener listener) {
this.mItemClickListener = listener;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView equipo_local, equipo_visitante, estado_video;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
jor= (TextView) itemView.findViewById(R.id.jornada);
estado= (TextView) itemView.findViewById(R.id.estado);
vi = (ImageView) itemView.findViewById(R.id.imageView1);
estado.setText(resultp.get(Resul.TAG_ESTADO));
jor.setText(resultp.get(Resul.TAG_JOR));
vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(view, position);
}
}
});
return itemView;
}
}
在你的适配器中尝试这样的接口:
public class ListViewAdapter{
public interface MyItemClickListener {
void onItemClick(View view, int position);
}
private MyItemClickListener mItemClickListener;
public void setMyItemClickListener(MyItemClickListener listener) {
this.mItemClickListener = listener;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Find your image view and add clicklistener
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view, int position) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(view, position);
}
}
});
return convertView;
}
}
现在您可以像这样在 activity 中设置您的 clicklistener:
// Pass the results into ListViewAdapter.java
adapter = new LazyAdapterResultadosLiga(getActivity(), arraylist);
// add click listener
adapter.setMyItemClickListener(new MyItemClickListener(){
@Override
public void onItemClick(View view, int position) {
Intent in = new Intent(getActivity(), MyOtherActivity.class);
startActivity(in);
}
});
// Set the adapter to the ListView
listview.setAdapter(adapter);