我想用2 textview创建一个简单的ListView。原始课程:https://github.com/thest1/LazyList/blob/master/src/com/fedorvlasov/lazylist/LazyAdapter.java
但是我有一个很大的问题:我在片段上工作,而不是在活动上。所以我调整了代码。
现在我在ListViewAdapter类中有一个错误(它不是我的主要类):
LayoutInflater inflater = context.getLayoutInflater();
"无法解决方法' getLayoutInflater()'
import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.pack.pack.R;
public class ListViewAdapter extends BaseAdapter
{
Fragment context;
String title[];
String description[];
public ListViewAdapter(Fragment context, String[] title, String[] description) {
super();
this.context = context;
this.title = title;
this.description = description;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.simplerow, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
return convertView;
}
}
我已经读过在BaseAdapter中无法正常工作。 我们怎么做到的? 谢谢。
编辑:我的listView类
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class commu extends Fragment {
public static final String TAG = "commu";
ListView lview;
ListViewAdapter lviewAdapter;
private final static String month[] = {"January","February","March","April","May",
"June","July","August","September","October","November","December"};
private final static String number[] = {"Month - 1", "Month - 2","Month - 3",
"Month - 4","Month - 5","Month - 6",
"Month - 7","Month - 8","Month - 9",
"Month - 10","Month - 11","Month - 12"};
@Nullable
//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.commu, container, false);
lview = (ListView) rootView.findViewById(R.id.listView);
lviewAdapter = new ListViewAdapter(this, month, number);
//System.out.println("adapter => "+lviewAdapter.getCount());
lview.setAdapter(lviewAdapter);
//lview.setOnItemClickListener(this);
return rootView;
}
/*public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(this,"Title => "+month[position]+"=> n Description"+number[position], Toast.LENGTH_SHORT).show();
}*/
@Override
public void onResume()
{
super.onResume();
//wv6.onResume();
}
@Override
public void onPause()
{
super.onPause();
//wv6.onPause();
}
}
答案 0 :(得分:15)
你可以使用
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
答案 1 :(得分:5)
使用LayoutInflater.from(Context)。确保将Activity的实例传递给方法。