更新时间:2023-11-05 17:30:28
如果你不使用机器人ID为的ListView
您无法通过 ListActivity
你必须将其与扩展活动
您应该使用机器人ID如果通过ListActivity扩展类
机器人:ID =@机器人:ID /名单
但是,如果您使用的是机器人:ID =@ + ID / DLIST
,那么你必须用扩展Activity类
另外你展示使用的setContentView(R.layout.displaylist);
获取的ID之前
列表=(ListView控件)findViewById(R.id.dlist);
list.setOnItemClickListener(itemclicklistener());
public class Display extends ListActivity
{
private ForumAdapter faHelper;
private ProgressDialog dialog;
private String Url;
private int success = 0;
private SeparatedListAdapter sAdapter;
private ListView list;
private List<Map<String,?>> cat_list;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTitle("Board Index");
list = (ListView) findViewById(R.id.dlist);
list.setOnItemClickListener(itemclicklistener());
setContentView(R.layout.displaylist);
faHelper = new ForumAdapter(this);
Url = geturl(savedInstanceState);
if (faHelper.isGood(Url))
{
Load();
}
else
{
alertdialog(0);
}
}
private class pbar extends AsyncTask<String, Void, Integer>
{
private final String message = "Loading. Please wait...";
@Override
protected void onPreExecute()
{
dialog = ProgressDialog.show(Display.this, "", message, true);
}
@Override
protected void onPostExecute(Integer result)
{
success = result;
dialog.dismiss();
displaystuff();
}
protected Integer doInBackground(String... url)
{
return faHelper.SourceDownload(url[0]);
}
}
private void displaystuff()
{
if (success == 1)
{
sAdapter = new SeparatedListAdapter(this, R.layout.demoheader);
for (Object row : faHelper.BoardIndex)
{
String[][] theRow = (String[][])row;
String ctitle = "";
cat_list = new LinkedList<Map<String,?>>();
for (int i = 0; i < theRow.length; i++)
{
if (i == 0) ctitle = theRow[i][0];
/* + "\n" + theRow[i][2]*/
cat_list.add(createObject(theRow[i][1], theRow[i][3]));
}
ExAdapter exone = new ExAdapter(this, R.layout.demorow, cat_list);
sAdapter.addSection(ctitle, exone);
}
list.setAdapter(sAdapter);
}
else
{
alertdialog(1);
}
}
private OnItemClickListener itemclicklistener()
{
return new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
@SuppressWarnings("unchecked") // Object to Map.
Map<String,?> one = (Map<String,?>) parent.getAdapter().getItem(position);
Toast.makeText(getApplicationContext(), one.get("title").toString(), Toast.LENGTH_SHORT).show();
}
};
}
private void alertdialog(int which)
{
switch (which)
{
case 0:
new AlertDialog.Builder(this)
.setTitle("Alert")
.setMessage("Connection Error!!!" + "\n\n" + faHelper.rstring)
.setPositiveButton("OK", new OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
Display.this.finish();
}
})
.show();
break;
case 1:
new AlertDialog.Builder(this)
.setTitle("Alert")
.setMessage("Error!!" + "\n\n" + "Press OK to try again!")
.setPositiveButton("OK", new OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
Load();
}
})
.setPositiveButton("Cancel", new OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
success = 1;
Display.this.finish();
}
})
.show();
break;
}
}
public Map<String,?> createObject(String title, String image)
{
Map<String,String> item = new HashMap<String,String>();
item.put("title", title);
item.put("image", image);
return item;
}
public void refresh(View view)
{
Load();
}
private void Load()
{
new pbar().execute(Url);
}
private String geturl(Bundle savedInstanceState)
{
String url = (savedInstanceState == null) ? null : (String) savedInstanceState.getSerializable("URL");
if (url == null)
{
Bundle extras = getIntent().getExtras();
url = extras != null ? extras.getString("URL") : null;
}
return url;
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putSerializable("URL", Url);
}
@Override
protected void onPause()
{
super.onPause();
}
@Override
protected void onResume()
{
super.onResume();
}
}
this line of code is for some reason creating a null pointer exception, and i have no idea why.
list.setOnItemClickListener(itemclicklistener());
when i just simply create a listview with code, it seems to work just fine. but when im actually setting it to be a listview inside a linearlayout, everything is fine except for the click listener. any advice would be help full.
the xml's are as follows:
displaylist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:onClick="refresh"
android:text="Refresh"
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="32dp">
</Button>
</LinearLayout>
<ListView
android:id="@+id/dlist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
header.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/demoheader_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:paddingLeft="5dip"
android:background="#FFFFFF"
style="?android:attr/listSeparatorTextViewStyle"
/>
row.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:layout_width="45dp"
android:layout_height="45dp"
android:width="50dp"
android:height="50dp"
android:scaleType="fitStart"
android:paddingRight="5dp"
/>
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
If you are not using androids id for ListView
you cannot extends the class by ListActivity
you have to extend it with Activity.
You should use Androids id if you extend Class by ListActivity
android:id="@android:id/list
But if you are using android:id="@+id/dlist"
then you have to extend class with Activity
Also you show use setContentView(R.layout.displaylist);
before fetching the id
list = (ListView) findViewById(R.id.dlist);
list.setOnItemClickListener(itemclicklistener());