我认为我快疯了。将自定义适配器绑定到Listview这样的简单事情居然让我头痛不已。
请发布代码并解释:
MainActivity.java
ListView从未显示。并且getView从未被使用,logCat没有显示“Inside Getview”。
有什么问题?
请发布代码并解释:
MainActivity.java
package com.example.pruebalist;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private static String[] data = new String[] {"0","1","2","3"};
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.v("MainActivity","Inside MainActivity");
setContentView(R.layout.main);
ListView lstView = (ListView)findViewById(R.id.listNoticias);
ArrayAdapter<String> adapter = new LstAdapter(this, R.layout.row, data);
lstView.setAdapter(adapter);
}
}
LstAdapter.java
package com.example.pruebalist;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class LstAdapter extends ArrayAdapter<String>{
private String[] mData;
private Context mContext;
int layoutResourceId;
public LstAdapter(Context context, int textViewResourceId, String[] values) {
super(context, textViewResourceId, values);
mContext = context;
mData = values;
layoutResourceId = textViewResourceId;
Log.v("LstAdapter","Inside LstAdapter");
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
Log.v("LstAdapter","Inside getView");
if(v==null){
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
v = inflater.inflate(layoutResourceId, parent,false);
}
String item = mData[position];
if(item!=null){
TextView txtItem = (TextView)v.findViewById(R.id.texto);
if(txtItem!=null){
txtItem.setText(item);
}
}
return v;
}
}
ListView从未显示。并且getView从未被使用,logCat没有显示“Inside Getview”。
有什么问题?