我的自定义适配器无法正常工作。我想显示一个listview,listview的每一行都有一个相对布局,里面有一个文本,一个edittext和两个按钮。我已经初始化了一串名字。字符串名称的元素将在listview的每一行中有所不同。 我是android的新手。请指出错误并提及为什么输出不可见。 这是代码
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
String[] names=new String[]{
"Name1" , "Name2" , "Name3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list);
listView.setAdapter(new CustomAdapter(this,names));
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Toast.makeText(
getApplicationContext(),((TextView) v.findViewById(R.id.text))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CustomAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
final String[] Name;
public CustomAdapter(Context context, String[] Name)
{
this.context=context;
this.Name=Name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listView;
if(convertView==null)
{
listView=new View(context);
listView=inflater.inflate(R.layout.activity_main,null);
TextView name=(TextView) listView.findViewById(R.id.text);
name.setText(Name[position]);
EditText editText=(EditText) listView.findViewById(R.id.editText);
editText.setHint("write here");
Button btn1=(Button) listView.findViewById(R.id.btnok);
name.setText("OK");
Button btn2=(Button) listView.findViewById(R.id.btncancel);
name.setText("cancel");
}
else
{
listView= (View) convertView;
}
return listView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Name.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/childlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.customlayout.MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/editText"
android:layout_width="400dp"
android:layout_height="50dp"
/>
<Button
android:id="@+id/btnok"
android:layout_below="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@+id/btncancel"
android:layout_below="@+id/editText"
android:layout_toRightOf="@+id/btnLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
listview.xml
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
答案 0 :(得分:0)
尝试改变它的改变,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_main, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.text);
name.setText(Name[position]);
EditText editText = (EditText) convertView.findViewById(R.id.editText);
editText.setHint("write here");
Button btn1 = (Button) convertView.findViewById(R.id.btnok);
name.setText("OK");
Button btn2 = (Button) convertView.findViewById(R.id.btncancel);
name.setText("cancel");
return convertView;
}
并删除
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return Name[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
答案 1 :(得分:-1)
试试吧,
{{1}}