您好我在尝试获取我创建的列表视图时遇到了一些麻烦,无法将用户输入添加到EditText视图中。当用户按下“添加”按钮但是程序刚崩溃时,应该添加内容,更不用说运行速度非常慢。我没有在下面添加我认为相关的部分。
public class ListViewActivity_Two extends Activity
{
EditText edit; Button add;
final List<String> data = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data.add( "Hello" );
edit = ( EditText )findViewById( R.id.editTxt );
add = ( Button ) findViewById( R.id.add );
add.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
data.add( edit.getText().toString() );
}
});
/**
for( int i = 1; i <= 10; i++ )
{
data.add( String.format( "Item %d", i ) );
}
*/
// Declare an instance of our custom adapter class
CustomAdapter adapter = new CustomAdapter( this, data );
ListView listView = ( ListView )findViewById( android.R.id.list );
listView.setAdapter( adapter );
listView.setOnItemClickListener( new OnItemClickListener()
{
@Override
public void onItemClick( AdapterView listview, View v, int pos, long id )
{
TextView textView = (TextView)v.findViewById( android.R.id.text1 );
toast( (String) textView.getText() );
}
});
}
data.add()就是为了确保我可以实际添加到ListView,我可以。我在onClickButton做同样的事情,为什么会发生这种情况?另外,我在onCreate方法上声明创建视图是正确的,还是必须在其中才能正常工作?
修改 的
这是CustomAdapter类
package com.stylingandroid.listview;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends BaseAdapter
{
private final Context context;
private final List<String> items;
public CustomAdapter( Context context, List<String> items )
{
this.context = context;
this.items = items;
}
@Override
public int getCount()
{
return items.size();
}
@Override
public Object getItem(int position)
{
return items.get( position );
}
@Override
public long getItemId(int position)
{
return getItem( position ).hashCode();
}
/**
* Method getView() below is inefficent because inflating XML layouts is memory expensive
* because it involves parsing XML and then instantiating objects to represent all of the Views
* in the view hierarchy
*
* see: http://blog.stylingandroid.com/archives/632
*
* for more details.
*
* The more efficient code is below it!
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
LayoutInflater inflater = ( LayoutInflater )context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View v = inflater.inflate( R.layout.item, parent, false );
final String item = ( String ) getItem( position );
TextView tv = ( TextView )v.findViewById( android.R.id.text1 );
tv.setText( item );
ImageView iv = ( ImageView )v.findViewById( R.id.imageView );
iv.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText( context, String.format( "ImageClicked: %s", item ), Toast.LENGTH_SHORT ).show();
}
});
return v;
}
*/
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
/**
* this method differs from the one above because, here, all we are doing is inflating a new view
* if convertView is null. This significantly reduces the amount of object deletion and instantiation
* that's required, and vastly improves our scrolling performance particularly on low powered devices
*/
View v = convertView;
if( v == null )
{
LayoutInflater inflater = ( LayoutInflater )context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate( R.layout.item, parent, false );
}
final String item = ( String ) getItem( position );
TextView tv = ( TextView )v.findViewById( android.R.id.text1 );
tv.setText( item );
ImageView iv = ( ImageView )v.findViewById( R.id.imageView );
iv.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText( context, String.format( "ImageClicked: %s", item ), Toast.LENGTH_SHORT ).show();
}
});
return v;
}
}
BTW我试图将程序的模拟器更改为API级别7;它完全破坏了程序。现在在每一个R.whatever我得到一个错误。我在Manifest文件中将它更改为7也一样重要吗?最后我清理了项目,但这没有什么区别 - 这里有什么问题?
答案 0 :(得分:1)
我实现了一些代码并且工作正常:)
ListView mListView;
EditText mValue;
Button mAdd;
List<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_listview);
data.add("Hello World");
mListView = (ListView) findViewById(R.id.custom_list);
mValue = (EditText) findViewById(R.id.text_to_add);
mAdd = (Button) findViewById(R.id.add_string);
mListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
mAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View currentView) {
data.add(new String(mValue.getText().toString()));
}
});
}
答案 1 :(得分:0)
我认为问题出在onClick上。这些变量的范围是不对的。对“数据”的引用不限于父类。与“编辑”相同。
相反,它的作用域是新的OnClickListner,因此未定义。
尝试使用
更改它以引用父类ParentClasName.this.data.add(ParentClasName.this.edit.getText().toString());
只需将“ParentClassName”替换为您的班级名称。