Here is my code - everything. I did as you told but I still can`t click on nothing. I mean I can click but nothing happens
package fixus.core;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import fixus.lib.News;
import fixus.testing.DataInput;
public class NewserActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataInput di = new DataInput();
ArrayList news = di.getNews();
NewsArrayAdapter naa = new NewsArrayAdapter(NewserActivity.this, R.layout.row, news);
setListAdapter(naa);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.i("testy", "I Clicked on Row " + position + " and it worked!");
}
});
}
@Override
/**
* When the user selects an item in the list, do an action
* @param ListView l
* @param View v
* @param int position
* @param long id
*/
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
final int index = position;
//You can add whatever you want to happen when you click here
Log.i("testy", "I Clicked on Row " + index + " and it worked!");
}
private class NewsArrayAdapter extends ArrayAdapter {
protected ArrayList items;
public NewsArrayAdapter(Context context, int textViewResourceId, ArrayList items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
News news = this.items.get(position);
if(news != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText("Name: "+ news.getName());
}
if(bt != null){
bt.setText("Status: "+ news.getUrl().toString());
}
}
return v;
}
}
}
If you know a good tutorial/example that would show me the good way I would love to see it :)
In your list activity what you should do is something like the following:
public class YourClass extends ListActivity {
//Your Variables
ArrayList<Type> yourlist;
YourAdapterClass adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
yourlist = new ArrayList<Type>();
this.adapter = new YourAdapterClass(this, R.layout.row, yourlist);
setListAdapter(this.adapter);
//you might be able to see if the below works instead of overriding
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("testy", "I Clicked on Row " + position + " and it worked!");
}
});
}
@Override
/**
* When the user selects an item in the list, do an action
* @param ListView l
* @param View v
* @param int position
* @param long id
*/
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
final int index = position;
//You can add whatever you want to happen when you click here
Log.i("testy", "I Clicked on Row " + index + " and it worked!");
}
//other methods can go here for you list
}
You don't want to have your onClickListener
inside your getView(...)
method in the List Adapter
, that is just where you are suppose modify the way your row looks like (adding buttons, textfields, etc. for each row) instead you want to have the onClickListener
in a class that extends ListActivity
that connects to your Adapter
Good luck, hope this helped
//Edit (adding more info)
You need to 'add it to your adapter' in the onCreate method (see edited code above) Hopefully that should fix the problem. Let me know if it still doesn't work though. If that doesn't work you can try using the setOnItemClickListener
code I put in the onCreate
method instead, if that still doesn't work I would try checking out some tutorials (I'll see if i can find you a good one)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With