Android 如何在修改的列表视图中设置特定行
对于如何为修改后的列表视图显示不同的xml布局,我有一个问题 “我的列表”视图充当注释部分 我只想为该评论的所有者显示一个编辑和删除按钮,如果他/她不是该评论的所有者,则不显示任何内容 这是我的示例代码 顺便说一句,我使用的是jsonAndroid 如何在修改的列表视图中设置特定行,android,xml,listview,Android,Xml,Listview,对于如何为修改后的列表视图显示不同的xml布局,我有一个问题 “我的列表”视图充当注释部分 我只想为该评论的所有者显示一个编辑和删除按钮,如果他/她不是该评论的所有者,则不显示任何内容 这是我的示例代码 顺便说一句,我使用的是json protected void onPostExecute(JSONObject json) { HashMap<String, String> user = session.sessionGetIdAccountType();
protected void onPostExecute(JSONObject json) {
HashMap<String, String> user = session.sessionGetIdAccountType();
String session_user_id = user.get(UserSessionManager.KEY_ID);
int integer_session_user_id = Integer.parseInt(session_user_id);
String user_id = "";
boolean usercomment = false;
try {
commentData = json.getJSONArray("list");
for (int i = 0; i < commentData.length(); i++) {
JSONObject source = commentData.getJSONObject(i);
Commenters commenter = new Commenters();
commenter.setName(source.getString("User_Fname") + " "
+ source.getString("User_Lname"));
commenter.setDate(source.getString("Comment_Date"));
commenter.setUsername(source.getString("User_Username"));
commenter.setComment(source.getString("Comment_Content"));
int integer_user_id = Integer.parseInt(user_id = source
.getString("User_ID"));
commenterList.add(commenter);
ListView listview = (ListView) findViewById(R.id.comment_list);
if (integer_session_user_id == integer_user_id) {
usercomment = true;
} else {
usercomment = false;
}
if (usercomment) {
adapter2 = new CommenterAdapter2(
getApplicationContext(), R.layout.row_user,
commenterList);
listview.setAdapter(adapter2);
} else {
adapter = new CommenterAdapter(getApplicationContext(),
R.layout.row, commenterList);
listview.setAdapter(adapter);
}
}
}
catch (Exception e) {
e.fillInStackTrace();
}
}
受保护的void onPostExecute(JSONObject json){
HashMap user=session.SessionGetIDACountType();
String session\u user\u id=user.get(UserSessionManager.KEY\u id);
int integer\u session\u user\u id=integer.parseInt(session\u user\u id);
字符串user_id=“”;
布尔usercomment=false;
试一试{
commentData=json.getJSONArray(“列表”);
对于(int i=0;i
希望这对您有所帮助
public class TestListViewAdapter extends ArrayAdapter<Object> {
static final int TYPE_HEADER = 0;
static final int TYPE_CELL = 1;
public TestListViewAdapter(Context context, List<Object> objects) {
super(context, 0, objects);
}
@Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return TYPE_HEADER;
default:
return TYPE_CELL;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
switch (getItemViewType(position)) {
case TYPE_HEADER: {
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tools_list_item_card_big, parent, false);
}
break;
case TYPE_CELL: {
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tools_list_item_card_small, parent, false);
}
break;
}
}
return convertView;
}
公共类TestListViewAdapter扩展了ArrayAdapter{
静态最终整数类型_头=0;
静态最终整数类型\单元=1;
公共TestListViewAdapter(上下文、列表对象){
超级(上下文,0,对象);
}
@凌驾
public int getItemViewType(int位置){
开关(位置){
案例0:
返回类型_头;
违约:
返回式单元;
}
}
@凌驾
public int getViewTypeCount(){
返回2;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
开关(getItemViewType(位置)){
案例类型_标题:{
convertView=LayoutInflater.from(parent.getContext())
.充气(R.layout.tools\u list\u item\u card\u big,parent,false);
}
打破
案例类型\单元:{
convertView=LayoutInflater.from(parent.getContext())
.充气(R.layout.tools\u list\u item\u card\u small,parent,false);
}
打破
}
}
返回视图;
}
}
在适配器开关的getview方法中,case用于在listview行上扩展不同的视图&getItemViewType()返回我要使用的视图。您也可以通过在数组列表自定义模型中添加属性来检查这一点
你能展示一下它现在的样子吗?