如何用自定义适配器替换我的基本适配器。这里我没有将我的代码更改为自定义适配器。至于我的代码,我应该如何用自定义适配器替换基本适配器以将消息动态地发送到自定义适配器。 下面我附上了我的代码,`
String[] monthsArray={
"https://www.google.co.in/maps?geo:0,0?q=my+street+address?geo:latitude,longitude"
};
mHistoryList = new ArrayAdapter<String>(this, android.R.layout.test_list_item);
//CustomListAdapter mHistoryList=new CustomListAdapter(this,links);
ListView hlv = (ListView) findViewById(R.id.useHistoryList);
hlv.setAdapter(mHistoryList);
CustomAdapter Code,
CustomListAdapter mHistoryList=new CustomListAdapter(this,monthsArray);
ListView list=(ListView)findViewById(R.id.useHistoryList);
list.setAdapter(mHistoryList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String SelectedItem=monthsArray[+position];
Toast.makeText(getApplicationContext(),SelectedItem,Toast.LENGTH_SHORT).show();
CustomAdapter.Java文件,
private final Context context;
private final String[] links;
public CustomListAdapter(Context context, String[] links) {
super(context,0,links);
this.context=context;
this.links=links;
}
public View getView(int position, View view, ViewGroup parent){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.link,null,true);
TextView textview=(TextView)rowView.findViewById(R.id.textview);
textview.setText(links[position]);
textview.setMovementMethod(LinkMovementMethod.getInstance());
return rowView;}}
我为listview和textview保留了单独的xml文件 Xml文件,
<ListView
android:id="@+id/useHistoryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:divider="#ff505050"
android:background="#ff202020"
android:dividerHeight="1dp"
android:transcriptMode="alwaysScroll"
/>
links.xml文件,
<TextView
android:id="@+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:autoLink="web"
>
</TextView>